Skip to main content
Inspiring
October 10, 2011
Answered

One-to-many Relationship

  • October 10, 2011
  • 2 replies
  • 1163 views

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

This topic has been closed for replies.
Correct answer crikos

Problem solved - ColdFusion seems to be too easy for Beginners ...

After reading the Documentation of Coldfusion it was not clear for me, how to setup a this Relationship. I tried it in that way, which was mentioned in a lot of Examples and also Books, and configured the Relationship on both sides (one-to-many in the first table, many-to-one in the referenced table).

Removing the many-to-one Relationship, which is in my case not needed, brought the result as expected.

Regards

2 replies

crikosAuthorCorrect answer
Inspiring
October 14, 2011

Problem solved - ColdFusion seems to be too easy for Beginners ...

After reading the Documentation of Coldfusion it was not clear for me, how to setup a this Relationship. I tried it in that way, which was mentioned in a lot of Examples and also Books, and configured the Relationship on both sides (one-to-many in the first table, many-to-one in the referenced table).

Removing the many-to-one Relationship, which is in my case not needed, brought the result as expected.

Regards

Inspiring
October 10, 2011

If you have a variable named x, of type User, x.id should give you the integer you want. 

crikosAuthor
Inspiring
October 10, 2011

Thanks for the answer.

I know, how to access the properties - regardless in which portion of the tree. But when I look at the Database table, normally I have stored in the user_id field of friends an integer Value, which corresponds to the users Table, where it is the Primary Key. But running that Query with ColdFusion brings me back friends Objects( = friends in user), where the field user_id is of type users (and not of type integer). Btw. every friend Object (in freinds of users) is the same than the parent of the friends array). Debugging in FlashBuilder shows, that every Dataset in user_id of the friends array has also a friends array, .... which seems to be an unfinite Loop.

Therefore I assume, that I have some problems in my CFCs.

Regards