-
Notifications
You must be signed in to change notification settings - Fork 40
CFMongoDB Object Mapper Syntax (proposed)
This is only proposed syntax!
There are 2 recommended ways to create persistent objects in a MongoDB datastore:
(1) Extending cfmongodb.components.MongoDocument, and
(2) Using the mongo.new_doc() factory method.
This provides 2 different approaches to generating the same types of objects. There are other ways, too [to do].
h4.Extending cfmongodb.components.MongoDocument
This is similar to CF9’s ORM syntax where you specify the fields you intend to persist. Though not required, this articulates what you intend to persist. MongoDocument has methods for storing and manipulating objects from the Mongo datastore – save(), update(), etc. [to do – link to API].
<cfcomponent displayName="MyBlog" output="true" extends="cfmongodb.components.MongoDocument"> <cfproperty name="title" /> <cfproperty name="author" /> <cfproperty name="pub_date" /> <cfproperty name="body" /> <cfproperty name="comments" /> <cfset super.init( collection_name='blog' ) /> <cffunction name="validate"> <!--- Insert custom validation code which will be executed prior to saves and updates --->
asdasdasda
To create a new MyBlog object and store it in Mongo, create the following client code:
<cfscript>
my_blog = createObject( "component", "MyBlog" );
my_blog.set( 'title', 'My New Blog Title' );
my_blog.set( 'author', 'bill' );
my_blog.set( 'pub_date', now() );
my_blog.set( 'body', 'My brilliant ideas ...' );
my_blog.save();
</cfscript>