class User extends AppModel
{
   var $name = 'User';

   var $hasMany = array(
                               'Message' =>array(
                                               'className'     => 'Message',
                                               'conditions'    => 'User.id = 
Message.from_id',
                                               'foreignKey'    => 'user_id'
                               )
                  );

}

From what I understand of the manual (though I may be wrong), the
'conditions' field  should be used to add extra condtions but not the
linking expression itself, which is generated by Cake. What's more,
when running a query from 'Message' it will look at the relationship
defined in 'Message' (the 'belongsTo' array) but not the one in 'User'
-- so your 'condition' is ignored. I think you should remove this
'conditions'.

To get the User records the primary key of which is equal to the
'from_id' in your Message, you should define the Message association
this way :

class Message extends AppModel {
   var $name = 'Message';
   var $belongsTo = array('User' =>
                                 array(
                                         'className'  => 'User',
                                         'foreignKey'   => 'from_id'
                                         )
                                    );
}

Then running
$message = $this->Message->findAll('Message.user_id=1');

Should return the expected data. Note that you can create new
associations on the fly (if you need different foreignKey) use
bindModel/unbindModel (see the manual chapter about models).

Anselm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to