Wednesday, March 2, 2011

How to properly use "conditions" in model associations

Hi everyone,

I'm wondering what is the 'proper' or 'suggested' way of using
"conditions" in model associations. I'm using an Activity model to log
activities performed throughout my app. I then use this table to
output activity feed / notifications style sections to users. The
problem I'm having is that Cake throws a "column not found" error (I
guess technically it's MySQL throwing the error). When running a
query, Cake for some reason formulates the query so that MySQL is
looking for columns from my Activity table inside of other tables. Do
I need to specify "conditions" in both my User and Activity model, as
shown below? When I remove the conditions from the Activity model,
everything runs fine.

<?php

class Activity extends AppModel {

var $name = 'Activity';
var $actsAs = array('Containable');

var $belongsTo = array(

// Doers
'User' => array(
'className' => 'User',
'foreignKey' => 'doer_id',
'conditions' => array('Activity.doer_table' => 'users')
)
}
?>

<?php

class User extends AppModel {

var $name = 'User';
var $actsAs = array('Containable');


var $hasMany = array(
'Activity' => array(
'className' => 'Activity',
'foreignKey' => 'doer_id',
'conditions' => array(
'Activity.doer_table' => 'users'
),
'dependent' => true
);
}
?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: