CF9+Air ORM - ManyToMany Relationships
I've been scouring the web for resources on ColdFusion 9's new syncmanager facilities for offline/online Air/Flex applications. To date there are only a couple blog posts and Adobe's live docs. Not much help.
I've been struggling lately just trying to get my ORM mapping on the Air side to work. In particular, ManyToMany relationships. I finally got it working about 2 seconds ago and thought I should blog this before I forget what the "trick" was.
First, here's a simplified table structure of my many-to-many relationship

Very basic.
Now according to the Adobe LiveDocs this should be coded as such in AS:
Companies.as
package com.myproject.model.vo
{
[]
[(alias="ServerSide.companies")]
[]
public class Companies
{
[]
public var ID:int;
public var account_statuses_ID:Number = 0;
public var sales_statuses_ID:Number = 0;
public var users_ID:Number = 0;
public var sales_rep_users_ID:Number = 0;
public var buying_groups_ID:Number = 0;
public var company_types_ID:Number = 0;
public var name:String = "";
//My ManyToOne relationship
[(targetEntity="Contacts", fetchType="EAGER")]
[(name="companies_to_contacts")]
[(name="companies_ID",referencedColumnName="ID")]
[(name="contacts_ID",referencedColumnName="ID")]
public var companyContacts:ArrayCollection;
}
}
Contacts.as
package com.myproject.model.vo
{
[]
[(alias="ServerSide.contacts")]
[]
public class Contacts
{
[]
public var ID:int;
public var name:String = "";
public var title:String = "";
public var addresses_ID:Number = 0;
public var contact_types_ID:Number = 0;
public var best_times:String = "";
public var created_datetime:Date = null;
public var created_by_users_ID:Number = 0;
public function Contacts()
{
}
}
}
companies_to_contacts.as
package com.myproject.model.vo
{
[(alias="ServerSide.companies_to_contacts")]
[]
public class companies_to_contacts
{
[]
public var contacts_ID:int;
[]
public var companies_ID:int;
public function companies_to_contacts()
{
}
}
}
However, this didn't work for me. I first got the error that the variable "companyContacts" must be an Array, not an ArrayCollection. Ok, changed that in Companies.as to:
public var companyContacts:ArrayCollection;
That fixed that problem, but it still didn't work. Kept complaining that variable "Contacts" didn't exist. This was referring to the targetEntity specified in Companies.as. After a little more digging in the Adobe LiveDocs I found this little gem. If you didn't follow that link, it basically deals with conflict managment. However the very last code snippet had a ManyToMany relationship setup, which had an odd way of specifying the targetEntity (odd to me, maybe this is normal?):
[(targetEntity="test::Product",cascadeType='ALL')]
Notice the "test::Product". That's the ticket. I changed my targetEntity in Companies.as to:
[(targetEntity="com.myproject.model.vo::Contacts", fetchType="EAGER")]
and viola, it worked!