Wednesday, December 2, 2009

Complex query issue, grouping and ordering

Hi there,

I'm having a problem with compiling a find/pagination query and was
hoping someone could help!

I have the a hasMany/belongsTo relationship between Users and Laps
(users HM laps), in the Laps table there is a lap_time field

And I'm trying to return the the lowest laptime for each user in in a
paginated array. So the result would be an array of unique users with
their lowest lap ordered by the laptime in ASC order.

Currently I am doing:

$this->paginate = array(
'limit' => 10,
'contain' => array('User(id,username,country_id)' => array
('Country(id,name,code)')),
'conditions' => $conditions,
'fields' => array('Lap.user_id','Lap.lap_time','MIN
(Lap.lap_time) as laptime'),
'group' => 'Lap.user_id',
'order' => 'laptime ASC'
);

$this->set('data', $this->paginate());

Which 'seems' to be working, altho the 'laptime' value is in a [0] key
outside of the [Lap] array.

Is this to be expected or am I doing something wrong?

TIA,

d.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: