Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:28
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save learncfinaweek/4121279 to your computer and use it in GitHub Desktop.
ORM - Hands On 18

In this hands on example, you are going to create the ORM entities for the Blog Section.

Functions Used: ormReload

  1. Open up the /www/Application.cfc file in your code editor.
  2. Locate the this.sessionTimeout variable declaration on or around line 6.
  3. 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;
  4. 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;	
    
  5. 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();
    	}
     }	
    
  6. Create a new file called blogPost.cfc in the /www/com/entity/ folder.
  7. Open up the /www/com/entity/blogPost.cfc file in your code editor.
  8. Declare the file as a component by adding the component script tags:
    component{
    }	
    
  9. 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"{
    }	
    
  10. 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";

  11. Create a new file called blogCategory.cfc in the /www/com/entity/ folder.
  12. 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>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment