Simple ORM concepts in coldfusion
ORM (Object Relational Mapping ) helps us to develop applications without writing repetitive SQL statements for select, update, insert and delete operations.
For configuring ORM, we should have a datasource in the server. Also we need to enable ORM in our Application.cfc file.
So the Application.cfc will look like,
component
{
this.name = "ORMtest"; //Name of the Application
this.datasource = "DatasourceName"; //setting datasource
this.ormenabled = true; //Enabling ORM
this.ormsettings.dbcreate = "update"; //Allows coldfusion to update database
}
For each tables in the database, we have to create different components or cfcs, which are called Entities. For example, if we have a table called 'Userdetails' , we can create a cfc 'userdetails.cfc', and we need to map the objects to tables.
component persistent="true" table="Userdetails"
{
property name="id" fieldType="id" ormtype="int" column="id" ;
property name="userfirstname" ;
property name="userlastname";
property name="address";
property name="description";
}
It's important to note that we should have an identifier and we have to mention the fieldtype attribute, like property name="id" fieldType="id" ormtype="int" column="id" ;
Tthe tag <cfproperty> , simply property is used to map objects.
We can dump the following code to see the new instance of the entity we have mapped.
<cfscript>
ORMReload();
udetails = new userDetails();
WriteDump(udetails);
</cfscript>
The output will look like,
Also, we can see the getters and setters for the entity by clicking on the methods in the ablove output.
Inserting data:
For inserting data we can use the following code,
<cfscript>
udetails = EntityNew( "userDetails" );
udetails.setid(2);
udetails.setuserfirstname("Firstname");
udetails.setuserlastname("Lastname");
udetails.setaddress("testing ORM");
udetails.setdescription("testing ORM");
entitysave(udetails);
WriteDump(udetails);
</cfscript>
the output will be
Fetching records:
for fetching records, below code can be used;
<cfscript>
udetails =EntityLoad( "userDetails",2, True );
writeDump( udetails );
</cfscript>
Updating records:
<cfscript>
udetails = EntityLoadByPK( "userDetails", 2 );
udetails.setdescription( "setting different description" );
writeDump( udetails );
</cfscript>
Output will be:
More: Ordering records using ColdFusion ORM
Deleting records using ColdFusion ORM
For configuring ORM, we should have a datasource in the server. Also we need to enable ORM in our Application.cfc file.
So the Application.cfc will look like,
component
{
this.name = "ORMtest"; //Name of the Application
this.datasource = "DatasourceName"; //setting datasource
this.ormenabled = true; //Enabling ORM
this.ormsettings.dbcreate = "update"; //Allows coldfusion to update database
}
For each tables in the database, we have to create different components or cfcs, which are called Entities. For example, if we have a table called 'Userdetails' , we can create a cfc 'userdetails.cfc', and we need to map the objects to tables.
component persistent="true" table="Userdetails"
{
property name="id" fieldType="id" ormtype="int" column="id" ;
property name="userfirstname" ;
property name="userlastname";
property name="address";
property name="description";
}
It's important to note that we should have an identifier and we have to mention the fieldtype attribute, like property name="id" fieldType="id" ormtype="int" column="id" ;
Tthe tag <cfproperty> , simply property is used to map objects.
We can dump the following code to see the new instance of the entity we have mapped.
<cfscript>
ORMReload();
udetails = new userDetails();
WriteDump(udetails);
</cfscript>
The output will look like,
Also, we can see the getters and setters for the entity by clicking on the methods in the ablove output.
Inserting data:
For inserting data we can use the following code,
<cfscript>
udetails = EntityNew( "userDetails" );
udetails.setid(2);
udetails.setuserfirstname("Firstname");
udetails.setuserlastname("Lastname");
udetails.setaddress("testing ORM");
udetails.setdescription("testing ORM");
entitysave(udetails);
WriteDump(udetails);
</cfscript>
the output will be
Fetching records:
for fetching records, below code can be used;
<cfscript>
udetails =EntityLoad( "userDetails",2, True );
writeDump( udetails );
</cfscript>
Updating records:
<cfscript>
udetails = EntityLoadByPK( "userDetails", 2 );
udetails.setdescription( "setting different description" );
writeDump( udetails );
</cfscript>
Output will be:
More: Ordering records using ColdFusion ORM
Deleting records using ColdFusion ORM
Comments
Post a Comment