In this hands on example, you are going to create the ORM entities for the Blog Section.
Functions Used: ormReload
- Open up the /www/Application.cfc file in your code editor.
- Locate the this.sessionTimeout variable declaration on or around line 6.
-
Below this, create the following new variables in the this scope:
- this.ormEnabled = true
- this.ormSettings = { logsql = true, dbcreate="update", cfclocation="com/entity" }
- this.invokeImplicitAccessor = true;
-
Your code should look similar to this:
this.sessionTimeout = CreateTimeSpan(0, 0, 30, 0); this.ormEnabled = true; this.ormSettings = { logsql = true, dbcreate="update", cfclocation="com/entity" }; this.invokeImplicitAccessor = true;
-
Go to the onRequestStart Function and add an ormReload() function call inside of the if statement. Your code should look similar to this:
function onRequestStart(string targetPage){ if(structKeyExists(url,'reload')){ onApplicationStart(); ormReload(); } }
- Create a new file called blogPost.cfc in the /www/com/entity/ folder.
- Open up the /www/com/entity/blogPost.cfc file in your code editor.
-
Declare the file as a component by adding the component script tags:
component{ }
-
Add an additional property to the component declaration, persistent, and set that value to true. Your code should look similar to this:
component persistent="true"{ }
-
Inside the component definition, enter the following code:
property name="id" column="blogpostid" fieldtype="id" generator="increment"; property name="title" ormtype="text"; property name="summary" ormtype="text"; property name="body" ormtype="text"; property name="dateposted" ormtype="timestamp"; property name="createdDateTime" ormtype="timestamp"; property name="modifiedDateTime" ormtype="timestamp"; property name="deleted" ormtype="boolean";
property name="comments" singularname="comment" fieldtype="one-to-many" cfc="blogComment" fkcolumn="blogpostid" cascade="all";
- Create a new file called blogCategory.cfc in the /www/com/entity/ folder.
-
Add the following code to the file:
component persistent="true"{ property name="id" column="blogCategoryid" fieldtype="id" generator="increment"; property name="name" ormtype="string"; }
</li> <li> Create a new file called <span class="code">blogComment.cfc</span> in the <span class="code">/www/com/entity/</span> folder. </li> <li> Add the following code to the file:
component persistent="true"{ property name="id" column="blogCommentid" fieldtype="id" generator="increment"; property name="author" ormtype="string"; property name="comment" ormtype="text"; property name="createdDateTime" ormtype="timestamp"; property name="deleted" ormtype="boolean"; property name="blog" fieldtype="many-to-one" cfc="blogPost"; }
</li> <li> Open up the <span class="code">/www/blog.cfm</span> page in your browser and append <span class="code">?reload=1</span> to the url. The <span class="code">ormReload</span> function will now have been called and the database tables will have been created. </li> </ol>