views:

213

answers:

1

Hi, I am using CakePHP framework to build a web application. The simplest form of my problem is this:

I have a users table and a messages table with corresponding models. Messages are sent from a user to another user. So messages table has columns from_id and to_id in it, both referencing to id of users. I am able to link Message model to User model by using $belongsTo but I am unable to link User model with Message model (in reverse direction) by using $hasMany in the same manner.

 var $hasMany = array(
  'From' => array(
   'className' => 'Message',
   'foreignKey' => 'from_id',
   'dependent' => false,
   'conditions' => '',
   'fields' => '',
   'order' => '',
   'limit' => '',
   'offset' => '',
   'exclusive' => '',
   'finderQuery' => '',
   'counterQuery' => ''
  ),
  'To' => array(
   'className' => 'Message',
   'foreignKey' => 'to_id',
   'dependent' => false,
   'conditions' => '',
   'fields' => '',
   'order' => '',
   'limit' => '',
   'offset' => '',
   'exclusive' => '',
   'finderQuery' => '',
   'counterQuery' => ''
  )
 );

What can do here? Any ideas? Thanks for any help.

+1  A: 

You can link the two models with 'hasMany' or 'belongsTo' more than one time with diffirent foreign keys at the same time.Since I don't find any example from the cookbook I tested that with a tiny piece of code and it worked as expected .

SpawnCxy