开发者

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')

)));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜