Question about model relationships

2007-03-01 Thread Anthony

OK, I'm aware this is probably a very dumb, newbie question, but you
gotta start somewhere, right?

Say I have a users table and a messages table. For each ONE user,
there are MULTIPLE messages. However, when doing a findAll on the
users, I would like it to spit back all the messages the user has both
sent AND received.

In the messages table, I am using belongsTo and the foreignKey is
set to the user ID that the message was sent TO. How can I tell the
model that the message also belongs to the user that sent the
message?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Question about model relationships

2007-03-01 Thread AD7six



On Mar 1, 6:02 pm, Anthony [EMAIL PROTECTED] wrote:
 OK, I'm aware this is probably a very dumb, newbie question, but you
 gotta start somewhere, right?

 Say I have a users table and a messages table. For each ONE user,
 there are MULTIPLE messages. However, when doing a findAll on the
 users, I would like it to spit back all the messages the user has both
 sent AND received.

 In the messages table, I am using belongsTo and the foreignKey is
 set to the user ID that the message was sent TO. How can I tell the
 model that the message also belongs to the user that sent the
 message?

In your message table:
create the field sender_id
rename user_id to recipient_id

In your Message model,
var $belongsTo = array(
'Sender'=array('className'='User','foreignKey'='sender_id'),
'Recipient'=array('className'='User','foreignKey'='recipient_id')
);

In your User model,
var $hasMany = array(
'SentMessages'=array('className'='Message','foreignKey'='sender_id'),
'ReceivedMessages'=array('className'='Message','foreignKey'='recipient_id')
);

In your User controller, after creating some dummy data,  put pr($this-
User-findAll()); and you should see the user, and both the messages
sent and received. If there are any errors in the above, check the api/
manual ;)

HTH,

AD


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Question about model relationships

2007-03-01 Thread Anthony

Thanks, that solved the problem!!

- Anthony


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---