I am wondering whether it is possible to create a hasMany Relationship in a Model which makes use of the ID of the logged in user.
Example:
One tip has many votings (from different users). This is easy:
public $hasMany = array(
'TipVoting' => array(
'className' => 'TipVoting',
'foreignKey' => 'tip_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
));
Now I want to get all Tips but want to know whether the logged in user hast voted this Tip already. So I would like to create a Realtionship which looks like this:
public $hasMany = array(
'MyTipVoting' => array(
'className' => 'TipVoting',
'foreignKey' => 'tip_id',
'dependent' => false,
'conditions' => array('TipVoting.user_id' => $loggedinUser_id),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
How do I get the $loggedinUser_id into this Model? Or is this a bad way of implementing this issue? Should I rather go for a seperate find on the TipVoting Model?
Any ideas?
Thanks!
Use the containable behavior. http://book.cakephp.org/view/474/Containable
You don’t want to define a different relationship, you just want to be able to filter by associated criteria. Containable will allow you to do this.
With just the TipVoting relationship, in your model do this:
in your controller action do this: