Object Relation mapping is one of the key feature in CFWheels. CFWheels makes things easier to start using ORM in your applications. The ORM configuration in CFWheels is implemented by creating a mapping between a database table and ColdFusion component in your application. To demonstrate ORM feature lets create a simple use case that list the available categories in a table with edit and delete links along a with add new category option.
First set the datasource name in settings.cfm file under config folder.
I'm using blogCFC database for this example and to map the table tblblogCategories, I have created a CFC under the model folder in my CFWheels framework and I named it as tblblogCategory.cfc. The thumb rule of ORM mapping in CFWheels is that the CFC name should be a singular version of your table name.
Also CFWheels gives us the flexibility of creating CFC's with other names as well to map a table. Here I have created CFC as category.cfc and to map the tblblogCategories here is the code I need to write.
One important point to note here is all the CFC's you're creating for database tables mapping should need to extend the Model component. We have done with the mapping now let's create some functions to perform CRUD operation in tblblogCategories table. Here is the code of the controller CFC CFWheelsORM.cfc.
I'm not using any form controls in this CRUD example hence I have used the values to createCatetory and editCategory as static. In the createCategory function I have created the new instance for my tblBlogCategory Model and passing the necessary params to it for to create a new category and calling the save() method to store the values to my database. In getCategory function I used the findAll() method and passing my SQL statement to it. If we didn't pass any SQL String then findAll() method will return all records from the table. By default findAll() method will return a query object we can also change this behavior to get an array of objects by setting like below. This will be very handy in many cases.
In editCategory and deleteCategory functions I have used another ORM function findByKey() as name denotes it will use to fetch data based on the parameter we have passed to it. The Update() and Delete() methods are used to update and delete data's from the database. CFWheels automatically convert all the inputs we passed into to the ORM methods to CFQueryParam. Like the Controller file name we need to create a folder under the CFWheels framework's view folder and then create new view file manageCategory.cfm in it with the following code.
I used the linkTo() function to create a link in my view page which links to the editCategory and deleteCategory actions by passing the categoryID as a key. We have done with a very basic ORM example in CFWheels and to learn more about CFWheels ORM features read the documentation at CFWheels site.
CFML/Web/Cloud/Data/Agile Enthusiast, Husband & Dad, an avid learner & android fan who works as a Tech Consultant in Toronto, Canada.