vibha
vibha

Reputation: 129

Find an array for hasMany relationship

I have relations like this:

On the project page, I want to display:

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?

Upvotes: 0

Views: 183

Answers (2)

chetanspeed511987
chetanspeed511987

Reputation: 2025

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

)));

Upvotes: 2

ianmjones
ianmjones

Reputation: 3415

Change 'all' to 'first' in your find, and as you already have a good 'order' you should be fine.

Upvotes: 3

Related Questions