Sunday, December 5, 2010

Re: Pagination using associated model record count?

On Sun, Dec 5, 2010 at 6:59 AM, Toby G <toby@tgpromotions.com> wrote:
> Hi there,
> Not sure where to start with this one, so offering it up for advice.
> I have 2 associated models...
> * Members
> * Events
> * Roles
>
> The Roles table links the members to events.
>
> What I would like to do is include the number of events a member has
> been involved with by including the Event count in the Members list
> (index) view, but would also like to be able to use the Paginator's
> magic to be able to sort by the number of events a member has been
> involved with.
>
> The models are setup as...
> Member hasMany Roles
> Event hasMany Roles
>
> Can anyone offer any advice on how to do this?

Have a look at counterCache.

http://book.cakephp.org/view/1031/Saving-Your-Data#counterCache-Cache-your-count-1033

You could store the number of Events per Member directly in the
members table. If I was doing this I'd use Containable and do
something like:

public $paginate = array(
'fields' => array('Member.*'),
'limit' => [some number],
'order' => array(
'Member.events_count' => 'DESC'
),
'contain' => array(
'Role' => array(
'Event'
)
)
);

>
> To make it a little more complicated some Members may perform multiple
> roles at 1 event, but I am interested in the number of unique Events
> for a Member, not the number of Roles they have performed.

In that case, you might be better off using HABTM associations.

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate