These are the resulting queries:

SELECT `User`.`id`, `User`.`username`, `User`.`name` FROM `users` AS
`User` ORDER BY `name` ASC

SELECT `Event`.`id`, `Event`.`date`, `EventsUser`.`id`,
`EventsUser`.`event_id`, `EventsUser`.`user_id` FROM `events` AS
`Event` JOIN `events_users` AS `EventsUser` ON (`EventsUser`.`user_id`
IN (3, 1, 2) AND `EventsUser`.`event_id` = `Event`.`id`) WHERE 1 = 1
ORDER BY `Event`.`date` DESC LIMIT 1

I actually don't know how to rewrite this query to get the latest
associated event (one) for each user...


On Sep 29, 3:30 pm, Bert Van den Brande <[email protected]> wrote:
> Have a look at the query it generates (set debug to 2) , I'm sure this will
> shine a bright light on the problem ...
>
> On Mon, Sep 28, 2009 at 9:48 PM, bram <[email protected]> wrote:
>
> > In my app, I have a User model that has a habtm association with
> > 'Event'. In one of the views, I want to show a list of Users and their
> > most recent event.
>
> > I ended up with this definition in the User model:
>
> >        var $hasAndBelongsToMany = array(
> >                'Event' => array(
> >                        'className' => 'Event',
> >                        'joinTable' => 'events_users',
> >                        'foreignKey' => 'user_id',
> >                        'associationForeignKey' => 'event_id',
> >                        'unique' => true,
> >                        'limit' => 1,
> >                        'order' => 'Event.date DESC'
> >                )
> >        );
>
> > find('all') returns the list of all users and only one of them has an
> > associated event.
>
> > When increasing the limit, it does show associated events, but when
> > some user don't have events associated, some other users will have
> > multiple events returned.
>
> > Is this a cake bug or do I have the wrong impression of the limit in a
> > habtm association?
>
> > Cheers,
>
> > Bram
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to