<<

ORM

Hands On 19

By Simon Free (Bio)
gist

In this hands on example, you are going to add the list and create functionality in the Admin for the blog posts and categories.

Tags Used: <cfset>

Functions Used: EntityLoad, EntityNew, now

  1. Open up the /www/admin/content/blog/editBlogPost.cfm file in your code editor.
  2. Locate the Edit Entity comment tag on or around line 39.
  3. Load the entity into a blogPost variable by adding the following code:
    <cfset blogPost = EntityLoad('BlogPost',form.id,true) />
    
  4. Set the blog post properties from the form scope by adding the following lines of code below the <cfset>:
    <cfset blogPost.title = form.title />
    <cfset blogPost.summary = form.summary />
    <cfset blogPost.body = form.body />
    <cfset blogPost.datePosted = form.datePosted />
    <cfset blogPost.modifiedDateTime = now() />	
    
  5. Next, add the logic to create a new blog post. Locate the comment tag that reads Create Entity on or around line 47.
  6. Create a new blog post object and set it to the blogPost variable by adding the following line of code:
    <cfset blogPost = EntityNew('BlogPost') />
    
  7. Set the object properties from the form scope by adding the following lines of code below the <cfset>:
    <cfset blogPost.title = form.title />
    <cfset blogPost.summary = form.summary />
    <cfset blogPost.body = form.body />
    <cfset blogPost.datePosted = form.datePosted />
    <cfset blogPost.createdDateTime = now() />	
    
  8. At this point, the blogPost object has all the information it needs and will be saved successfully. The next step is pull the blog post information for the edit form. To do this, find the Get Entity Data comment tag.
  9. Create a blog Post object using the URL variable ID and set it to the blogPost variable by adding the following line of code:
    <cfset blogPost = EntityLoad('BlogPost',url.id, true ) />
    
  10. Now set the blogPost properties into the form scope so that they will display in the form. To do this, add the following lines of code below the <cfset> tag:
    <cfset form.id = blogPost.id />	
    <cfset form.title = blogPost.title />
    <cfset form.summary = blogPost.Summary />
    <cfset form.body = blogPost.body />
    <cfset form.datePosted = blogPost.datePosted />
    
  11. The add/edit process is now complete, but before you test it, update the listing page to pull the entities. Open up the /www/admin/content/blog/listblogpost.cfm file in your code editor.
  12. Find the comment Pull Blog Posts and add a <cfset> below, that calls EntityLoad on blogPost and sets the value to a variable called blogPosts. Do this by adding the following line of code:
    <cfset blogPosts = EntityLoad('BlogPost') />	
    
  13. Next, output the information to the user. Find the Title comment tag and output the title property of the blogPost object. Do this by adding the following code below the Title comment tag:
    #blogPost.title#	
    
  14. Then, output the datePosted value; add the following line of code below the Date Posted comment tag:
    #dateFormat(blogPost.datePosted,"mm/dd/yyyy")#	
    
  15. The final stop to complete the listing page is to output the blog post ID in the edit link so that the edit page knows which blog post is being edited. To do this, append the following line of code to the ID URL attribute in the <a> tag under the Edit Post comment:
    #blogPost.id#
    
  16. The admin is now able to create and edit blogPosts. Navigate to /www/admin/ in your browser and login with the Email Address of admin@myWebsite.com and password of admin. Once logged in, click on 'Blog'.
  17. Click on 'Add Blog Post' and create a new blog.
  18. Go back to the listing page and edit your blog.

Homework

Update the blogCategory files in the admin so that blog categories can be create and edited.