I was just wondering why you need to specify a dialect in ORM, if you define this in your datasource?
Example:
Application.cfc
this.ormSettings = { datasource="jbankenBlogspot",dialect="MySQLwithInnoDB" };
I shouldn't have to specify dialect="MySQLwithInnoDB" if I did it already in my datasource. Maybe I am missing something here.
The real hard part is that hibernate was throwing an error that was the same as if you had a bad path to your entity cfc.
Monday, July 12, 2010
Wednesday, July 7, 2010
ColdMVC: Parse checkboxes or radios generically
I ran into an interesting issue awhile, back. I wanted to edit a product and click on checkboxes for one to many relationships to colors, categories, and sizes. When I post the form to the ProductController save() I wanted a generic way to convert checkbox values (which are ids) to actually objects. Below are the steps I took followed by the code.
First, I call private functions to parse the specific checkbox ids ( Ex. parseCategories()), but they all just call parseResource().
Next, while in parseResource() I look into the variables scope for the model (Ex. _Size) to dynamically get the object by using findByID().
Lastly, I append the object to an array and populate the Product object and save it.
ProductController.cfc
I wanted to share this just in case someone else is running into the issue.
First, I call private functions to parse the specific checkbox ids ( Ex. parseCategories()), but they all just call parseResource().
Next, while in parseResource() I look into the variables scope for the model (Ex. _Size) to dynamically get the object by using findByID().
Lastly, I append the object to an array and populate the Product object and save it.
ProductController.cfc
/**
* @accessors true
* @action list
* @extends coldmvc.Controller
*/
component {
property _Size;
property _Color;
property _Category;
function save() {
var product = _Product.new();
params.product.categories = parseCategories(params.product.categories);
params.product.sizes = parseSizes(params.product.sizes);
params.product.colors = parseColors(params.product.colors);
product.populate(params.product);
product.save();
redirect({controller="product",action="setup"},"productID=#product.id()#");
}
private array function parseCategories(string categoryIDs) {
return parseResource("Category",arguments.categoryIDs);
}
private array function parseSizes(string sizeIDs) {
return parseResource("Size",arguments.sizeIDs);
}
private array function parseColors(string colorIDs) {
return parseResource("Color",arguments.colorIDs);
}
private array function parseResource(string resource, string resourceIDs){
var resources = $.string.toArray(arguments.resourceIDs);
var result = [];
var i = "";
for (i=1; i <= arrayLen(resources); i++) {
arrayAppend(result, variables["_#arguments.resource#"].findByID(resources[i]));
}
return result;
}
}
I wanted to share this just in case someone else is running into the issue.
Thursday, June 24, 2010
CSS hierarchy wish
Alot, of times I find myself having a wrapper around some elements in my html so that I can do some custom css inside of it. In the css case below the wrapper is a div with a class of "webfolio".
div.webfolio h3{
font-size:1.5em;
color:#333;
}
div.webfolio div.article{
border:1px solid #666;
}
div.webfolio div.description{
color:#555;
}
Notice I have to write "div.webfolio" over and over. I just want to write it once, with everything (it's children) inside of it. I wish I could do it like this:
div.webfolio{
h3{
font-size:1.5em;
color:#333;
}
div.article{
border:1px solid #666;
}
div.description{
color:#555;
}
}
I just feel dirty writing the same name over and over. Yes, I realize if you get more than one or two deep in above css it would get harder to read. In a perfect world this could be prevented by code responsibility.
div.webfolio h3{
font-size:1.5em;
color:#333;
}
div.webfolio div.article{
border:1px solid #666;
}
div.webfolio div.description{
color:#555;
}
Notice I have to write "div.webfolio" over and over. I just want to write it once, with everything (it's children) inside of it. I wish I could do it like this:
div.webfolio{
h3{
font-size:1.5em;
color:#333;
}
div.article{
border:1px solid #666;
}
div.description{
color:#555;
}
}
I just feel dirty writing the same name over and over. Yes, I realize if you get more than one or two deep in above css it would get harder to read. In a perfect world this could be prevented by code responsibility.
Tuesday, May 25, 2010
From SQL to HQL
Most of the development I have done has been with SQL records. Now that CF9 is out I've started playing around with HQL objects and it has been a learning curve but for the best. I struggled and continue to struggle with writing HQL, because my mind still wants to do SQL. I wanted to share what I have learned so far.
Given the db schema below I will show how to write a piece in sql followed by a piece in hql.
There is a PRODUCT table with a many to many relationship with the SIZE table and the PRODUCT table has many to many relationship with the CATEGORY table.
PRODUCT -|-----= PRODUCT_SIZE =------|- SIZE
PRODUCT -|-----= PRODUCT_CATEGORY =-----|- CATEGORY
1. Get all the products
SQL
HQL
2. Get all the products with price equal to $100.
SQL
HQL
3. Get all the products with size_id = 1.
SQL
HQL
4. Get all the products with size_id = 1 and category_id = 1.
SQL
HQL
Note: HQL can't solve all queries, a good example is Accounting queries, but it does help with the majority of the light weight queries. I get the line "Well what's the point of using objects if records work fine.". I reply with "There is no point, but if you want to update all your queries with new properties, you will quickly find the point."
Resources used:
http://www.barneyb.com/barneyblog/2010/05/05/embrace-your-hsql/
Given the db schema below I will show how to write a piece in sql followed by a piece in hql.
There is a PRODUCT table with a many to many relationship with the SIZE table and the PRODUCT table has many to many relationship with the CATEGORY table.
PRODUCT -|-----= PRODUCT_SIZE =------|- SIZE
PRODUCT -|-----= PRODUCT_CATEGORY =-----|- CATEGORY
1. Get all the products
SQL
select * from product
HQL
from Product
2. Get all the products with price equal to $100.
SQL
select * from product where price = '100'
HQL
from Product where price = '100'
3. Get all the products with size_id = 1.
SQL
select * from product
inner join product_size on product.id = product_size.product_id
where product_size.size_id = 1
HQL
select product
from Product product
join product.sizes size
where size.id = 1
4. Get all the products with size_id = 1 and category_id = 1.
SQL
select * from product
inner join product_size on product.id = product_size.product_id
inner join product_category on product.id = product_category.product_id
where product_size.size_id = 1
and product_category.category_id = 1
HQL
select product
from Product product
where exists (
from product.categories category
where category.id = 1
)
and exists (
from product.sizes size
where size.id = 1
)
Note: HQL can't solve all queries, a good example is Accounting queries, but it does help with the majority of the light weight queries. I get the line "Well what's the point of using objects if records work fine.". I reply with "There is no point, but if you want to update all your queries with new properties, you will quickly find the point."
Resources used:
http://www.barneyb.com/barneyblog/2010/05/05/embrace-your-hsql/
Sunday, April 11, 2010
ColdMVC: Create your own Helpers.
I wanted to try extending ColdMVC's helpers and create my own helper that formatted stuff for me and it turned out to be quite simple.
First, I created my own directory in my project called "helpers".
Second, I added a cfc named "format".
Third, I extended the ViewHelper from ColdMVC. Not sure, If this is the correct Helper Util class I am suppose to be using...but it worked.
Lastly, I used my new helper function:
If you noticed in money() I set a default value of zero. For some odd reason when I put "product.getPrice()" in for the amount argument it has a value of [empty string] and it throws an error. The error says "The AMOUNT parameter to the money function is required but was not passed in.". But clearly there is a value of [empty string]. If any one knows what up give me hollar.
First, I created my own directory in my project called "helpers".
Second, I added a cfc named "format".
Third, I extended the ViewHelper from ColdMVC. Not sure, If this is the correct Helper Util class I am suppose to be using...but it worked.
<cfcomponent extends="coldmvc.utils.ViewHelper">
<!------>
<cffunction name="money" access="public" output="false" returntype="any">
<cfargument name="amount" required="false" default="0"/>
<cfreturn dollarFormat(arguments.amount)/>
</cffunction>
<!------>
</cfcomponent>
Lastly, I used my new helper function:
#$.format.money(product.getPrice())#
If you noticed in money() I set a default value of zero. For some odd reason when I put "product.getPrice()" in for the amount argument it has a value of [empty string] and it throws an error. The error says "The AMOUNT parameter to the money function is required but was not passed in.". But clearly there is a value of [empty string]. If any one knows what up give me hollar.
Entity Name is same for two CFCs
As I have been trying out CF9 and the ColdMVC I occasionally get this error "Entity Name Category is same for two CFCs". Where "Category" is my model cfc. This is ok if I really had two CFC's with the same name. Here's the rest of the error:
Entity Name Category is same for two CFCs, superior.app.model.Category and superior.app.model.Category.
As you can see, the error looks like it's pointing at the same file. Searching up and down my Eclipse project explorer I find no two cfc with the same name. Guess what...do a refresh on your model directory or where ever you put your VO's. You probably have two hibernate files, both containing your VO's name...mine was "Category".
Get rid of one of them and it should work.
Entity Name Category is same for two CFCs, superior.app.model.Category and superior.app.model.Category.
As you can see, the error looks like it's pointing at the same file. Searching up and down my Eclipse project explorer I find no two cfc with the same name. Guess what...do a refresh on your model directory or where ever you put your VO's. You probably have two hibernate files, both containing your VO's name...mine was "Category".
Get rid of one of them and it should work.
Tuesday, April 6, 2010
Exploring ColdMVC
If you haven't checked out ColdMVC yet you should. It's a light weight ColdFusion ORM framework I've being playing around with. The biggest thing I was impressed with is the tags on the views you can make...I am easily satisfied.
Have you ever been tired of typing that same css container.
Ex:
With ColdMVC's tags that section turns into:
The tags help keep the view clean. The only worry that I have NOT ran into yet is that helper tags are very similar to html tags. Not sure how it will play out yet, but no problems so far.
Have you ever been tired of typing that same css container.
Ex:
<div class="buttons">
<!--- some buttons --->
</div>
With ColdMVC's tags that section turns into:
<buttons>
<!--- some buttons --->
</buttons>
The tags help keep the view clean. The only worry that I have NOT ran into yet is that helper tags are very similar to html tags. Not sure how it will play out yet, but no problems so far.
Subscribe to:
Posts (Atom)