I am trying to define the relationship between tables using ColdFusion 9.0.1 ORM.
I have 2 tables: users and companies
I am working on the relationship between users to companies. Right now I have this:
component persistent="true" table="users" datasource="core"
{
property name="userID" fieldtype="id" column="userID"
setter="false" generator="native";
property name="firstName" length="255";
property name="lastName" length="255";
property name="userName" length="45";
property name="companies" fieldtype="many-to-one" cfc="company"
fkcolumn="companyID" singularname="company";
}
and
component persistent="true" table="companies" datasource="core"
{
property name="companyID" fieldtype="id" column="companyID"
setter="false" generator="native";
property name="companyName" length="255";
property name="companyCode" length="45";;
property name="users" fieldtype="one-to-many" fkcolumn="userID" cfc="user"
cascade="all" inverse="true";
}
I am having an issue with writing to the user table. I need to write a companyID into the user table so that I can link back to the company. On this insert I do not want insert anything into the companies table. I just want to write to the users table and have it link to the companies table via a companyID in the users table.
Here is my EntitySave code:
transaction{
newUser = EntityNew("user");
newUser.setCompanies(#FORM.companyID#);
newUser.setFirstName(#FORM.firstName#);
newUser.setLastName(#FORM.lastName#);
newUser.setUserName(#FORM.userName#);
EntitySave(newUser);
}
When I run it like that I get this error "The value for property java.lang.String cannot be retrieved from object of type companyID. Expected object type is company."
I was thinking maybe I need to have separate property for the companyID like this "property name="companyID" ormType="int" and then maybe I could use "newUser.setCompanyID(#FORM.companyID#);" in the EntitySave but that does not work. I get an error on the ORMReload(); when I have that in there.
I am obviously pretty confused about the best way to setup relationships. I can get this working without trying to officially reference the relationship in the user.cfc file and just using property name="companyID" ormType="int" but that does not give me a proper relationship between the tables. I think?
Any help on this would be great.
Thanks in advance.
Try changing this:
newUser.setCompanies(#FORM.companyID#);
to this:
newUser.setCompanies(EntityLoadByPK ("company", FORM.companyID));
what's with the last "true"? I don't think it is needed unless you use EntityLoad() –
EntityLoadByPK ("company", FORM.companyID)
returns an array containing all entities that conform to the criteria (pk=FORM.companyID)
EntityLoadByPK ("company", FORM.companyID, true)
returns an object that conforms to the criteria (pk=FORM.companyID)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.