Saturday, April 2, 2011

Re: Paginate pre-ordered list

So is this correct?

The 'likes' table has one record for each time a User is liked (i.e.
if 50 people have indicated that they like the User with user_id = 23,
then there are 50 records in the 'likes' table with user_id = 23), and

User hasMany Like
Like belongsTo User

If so, have you considered using the counterCache option of the
belongsTo definition? (see: http://book.cakephp.org/view/1042/belongsTo).
Basically, with the counterCache, any insert/update/delete in Model
(in this case, Like), will automatically update the designated field
(default: like_count) in the Model that it belongsTo (User) with the
number of related rows. You can then use the field (User.like_count)
in the 'order' option of Controller::paginate.

On Apr 1, 10:09 am, CrotchFrog <crotchf...@gmail.com> wrote:
> @ShadowCross:
>
> I originally tried your example of:
>
> [code]
> $this->paginate('Like', array(
>     'contain' => array('User'),
>     'conditions' => array(
>         'Like.count >' =>  0
>     ),
>     'order' => array(
>         'Like.count' => 'desc',
>         'Like.modified' => 'asc'
>     )
> );
> [/code]
>
> The problem I faced there is that User $hasMany Like, not $hasOne.
> Using the single record containing "Like.count" was an afterthought to
> be able to order my results from greatest to least (that is all it's
> used for and to be honest I would much rather not do it that way). For
> each "like" two things happen, a new record is created for the "like"
> and the record containing "count" is incremented by one. With using
> $hasMany and containable Like ... in the view I loop through the array
> and if in_array('your session id, $user['Like']), you like that user.
> Also count($user['Like']) ... how many times that user is "liked."
>
> I could actually switch to $hasOne without creating much of an issue
> in other areas of the app but then I lose the array of "likes" for
> each user as I loop through the set. Looks like I have six of one,
> half dozen of the other.
>
> On Mar 31, 1:53 pm, ShadowCross <adri...@jps.net> wrote:
>
>
>
> > If I'm interpreting it correctly, you have a table 'likes' that has the
> > following fields:
>
> >   user_id (unique, represents the user_id of the person who is liked)
> >   count (number of users who indicated that they like this user_id)
>
> > so that Like belongsTo User, and conversely, User hasOne Like. Assuming that
> > both User and Like have the same dataSource, have you tried:
>
> > $this->paginate('User', array(
> >     'contain' => array('Like'),
> >     'conditions' => array(
> >         'Like.count >' => 0
> >     ),
> >     'order' => array(
> >         'Like.count' => 'desc',
> >         'Like.modified' => 'asc'
> >     )
> > );
>
> > instead of using Like::getMostLiked() then running individual queries for
> > each user in the resulting array.
>
> > Because User hasOne Like, the ContainableBehavior uses a LEFT JOIN between
> > User and Like, so Like.count is in the scope of the SQL query, and you
> > should be able to use it as part of the $options['order'].  Or as an
> > alternative, you can also try:
>
> > $this->paginate('Like', array(
> >     'contain' => array('User'),
> >     'conditions' => array(
> >         'Like.count >' =>  0
> >     ),
> >     'order' => array(
> >         'Like.count' => 'desc',
> >         'Like.modified' => 'asc'
> >     )
> > );
>
> > Either one should work, since there's a one-to-one relation between the two
> > models.  And since only one SQL query is executed (instead of 1 + $limit,
> > because you currently run 1 SQL query for Like::getMostLiked, and then
> > individual queries for each of the user_id values), it should be a little
> > faster and less resource-intensive.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: