Find an array for hasMany relationship
I have relations li开发者_开发百科ke this:
- Project has multiple Properties
- Property has multiple Rates
On the project page, I want to display:
- The project, its properties and its latest rate
So far, I have this:
$this->set('rates', $this->Rate->find('all', array(
'conditions' => array('Property.project_id' => $id),
'fields' => array('Rate.id', 'Rate.rate', 'Rate.floor_rise', 'Property.id'),
'order' => array('Rate.created DESC')
)));
This gives all rates for the respective property, but I only want the latest rate.
How should I do this query?
Change 'all'
to 'first'
in your find, and as you already have a good 'order'
you should be fine.
$this->set('rates', $this->Rate->find('first', array(
'conditions' => array('Property.project_id' => $id),
'fields' => array('Rate.id', 'Rate.rate', 'Rate.floor_rise', 'Property.id'),
'order' => array('Rate.created DESC')
)));
精彩评论