ColdFusion 8: how to implement (one/many)-to-one for Managed Associations
LCDS provides 2 ways of feeding your application with complex objects. Those objects can be hierarchical or managed. When dealing with hierarchical objects filled from a destination, that destination is responsible for committing all changes back to the database.
Managed objects work differently. When using managed associations, you define in your destination's configuration which destination will be responsible for any changes made to that object.
Taking TomJ CRM example we could have in the cfemployee this definition:
...
<metadata>
...
<many-to-one property="company" destination="cfcompany"/>
...
</metadata>
</destination>
When retrieving data from the cfemployee destination, if you change any property in the company property, those changes will be committed to the cfcompany destination instead.
How do I implement necessary changes in my files so I can use these kind of relationships?
While the Flex Builder plugins generate most of my code, they don't support these kinds of objects.
This is what needs to be changed in your objects so you can use one-to-one, many-to-one, one-to-many and many-to-many relationships. BTW, one-to-one and many-to-one are the same because LCDS just takes care of the last part of the relationship (-to-one and -to-many).
First, I'll use the employee example having one related object (company)
in the Employee.cfc (your VO)
you should add
<!--- cfscript block --->
<cfscript>
this.company = javacast('null','');
</cfscript>
<!--- getters and setters --->
<cffunction name="setCompany" output="false" access="public">
<cfargument name="val" type="any">
<cfset this.company = arguments.val>
</cffunction>
<cffunction name="getCompany" output="false" access="public"
returntype="any">
<cfif NOT isDefined("this.company")>
<cfreturn javacast("null", "")>
</cfif>
<cfreturn this.Company>
</cffunction>
In your EmployeeDAO.cfc you should provide a way to populate your company property in the read method. You can achieve this by using the companyDAO and read it by id (like the get method does)
Add this at the beginning of the read method
and in the cfscript block
obj.setPhone(qRead.phone);
obj.setTitle(qRead.title);
if(qRead.companyId gt 0){
obj.setCompany( myCompanyDao.read(id=qRead.companyId) );
}
Then you will have to change your AS3 class so it contains your related object.
{
[Managed]
[RemoteClass(alias="samples.crm.Employee")]
public class Employee
{
public function Employee() {}
public var employeeId:int;
public var firstName:String = "";
public var lastName:String = "";
public var title:String = "";
public var phone:String = "";
public var email:String = "";
public var company : Company=null;
}
}
You should initialize company using javacast instead of ='' because if the object should be passed as a null object to the flex client, the app will not be able to see it as one if you return that property as an empty string and you will get a cohersion error in your application.


There are no comments for this entry.
[Add Comment]