I have following models: - Agenda (id, user_id, event_id) -> belongsto Event, User - Event (id, etc) -> hasmany Agenda - User (id, city_id, etc) -> hasmany Agenda, belongsto City - City (id, name) -> hasmany User
While in the Agendas controller, I want to display the City.name of an user that has a certain event开发者_如何转开发_id in his Agendas. How can I add City data to the User array of the Agendas array?
N.B. I don't want to use recursive 2 because that loads way to much data into the array.
Your best option looks to be the CakePHP Containable Behaviour http://book.cakephp.org/view/474/Containable
It allows you to limit the associated model data that is returned - it basically has the power of recursive 2 in being able to find deep association data, but has much better performance and is cleaner because it gives you only the data you need.
Something like the following code should return the City data within the User data, including the condition of event_id.
$this->Agenda->find('all', array(
'contain' => array(
'User' => array(
'City'
),
),
'conditions' => array(
'Agenda.event_id' => $event_id
)
));
Just to complete that code snippet, ensure var $actsAs = array('Containable');
is at the top of the model (or in the app_model.php if you want the containable behaviour accessible to all models)
精彩评论