One-to-many Relationship
Hi Everybody,
i just started with ColdFusion and am trying some general tasks. On one of those tasks I have problems, which brought me to this forum.
I am developing a project with FlashBuilder 4.5, Coldfusion Standard 9.0.1. and MySQL5
In my Project I have two simple Tables - users and friends, where 1 user has n friends and many friends have 1 user (one-to-many, many-to-one).
My CFCs are :
users.cfc:
component persistent="true" table="users" entityName="users"{
property name="id" column="id" ormtype="integer" type="numeric" fieldtype="id" generator="increment" length="10" ;
property name="username" column="username" ormtype="string" type="string" length="255" ;
property name="email" column="email" ormtype="string" type="string" length="50" ;
property name="firstname" column="firstname" ormtype="string" type="string" length="50" ;
property name="lastname" column="lastname" ormtype="string" type="string" length="50" ;
public users function init() {
return This;
}
public void function nullifyZeroID() {
if (getid() eq 0 OR getid() eq ""){
setid(JavaCast("Null", ""));
}
}
public users function populate(required struct formStruct ) {
if (StructKeyExists(arguments.formstruct, "id") AND arguments.formstruct.id > 0){
This = EntityLoad("users", arguments.formstruct.id, true);
}
if (StructKeyExists(arguments.formstruct, "username")){
this.setusername(arguments.formstruct.username);
}
if (StructKeyExists(arguments.formstruct, "email")){
this.setemail(arguments.formstruct.email);
}
if (StructKeyExists(arguments.formstruct, "firstname")){
this.setfirstname(arguments.formstruct.firstname);
}
if (StructKeyExists(arguments.formstruct, "lastname")){
this.setlastname(arguments.formstruct.lastname);
}
return This;
}
}
and friends.cfc:
component persistent="true" table="friends" entityName="friends"
{
property name="id" column="id" type="numeric" ormtype="integer" generator="increment" fieldtype="id" ;
property name="friend_id" column="friend_id" type="numeric" ormtype="integer" ;
property name="user_id" fieldtype="many-to-one" fkcolumn="user_id" type="numeric" ormtype="integer" cfc="users" remotingfetch="true" missingrowignored="true" ;
public friends function init() {
return This;
}
public void function nullifyZeroID() {
if (getid() eq 0 OR getid() eq ""){
setid(JavaCast("Null", ""));
}
}
public friends function populate(required struct formStruct ) {
if (StructKeyExists(arguments.formstruct, "id") AND arguments.formstruct.id > 0){
This = EntityLoad("friends", arguments.formstruct.id, true);
}
if (StructKeyExists(arguments.formstruct, "user_id")){
this.setuser_id(arguments.formstruct.user_id);
}
if (StructKeyExists(arguments.formstruct, "friend_id")){
this.setfriend_id(arguments.formstruct.friend_id);
}
return This;
}
}
requesting one User with:
remote users function getusers( id )
{
var primaryKeysMap = { id = id };
return entityload("users",primaryKeysMap,true);
}
brings back a correct Set of data, where the property "friends" in user is an Array, with all friends of user. friends is an Array of Type friends.
My Problem is that in the friends Array the field for the Foreign Key (in my case user_id) will be populated with User Objects, and not with integer Values, coressponding to the id in users Table.
Trying to update the User results in en Error on the ColdFusion Server.
How can tell ColdFusion to put the Integer Values (for the ID of User) in the friends Objects ?
Thanks for helping,
Regards
