Wednesday, April 1, 2009

Re: paginateCount and SELECT DISTINCT

BUMP.

I'm wondering too.
It is pity that DISTINCT not applies to COUNT as well.
I'm trying to solve this issue as well. However if I replace the
paginateCount() method in my model, then it will be replaced for all
my pagination queries, isn't it?
I don't think this is the best solution out there. Any more usable
hints would be really appreciated.

On Mar 16, 8:15 pm, Fibra <fabricio.sod...@gmail.com> wrote:
> Hello everyone, how are you?
>
> If you ever had the misfortune of finding yourself in the need of
> using the $paginate variable with a DISTINCT operation, you must have
> found out that the returned amount of records is not correct! It
> returns the COUNT of all the records (ignoring the DISTINCT in the
> fields array) instead of the COUNT(DISTINCT id), for example.
>
> I've been looking all over the web for a solution to this particular
> case of pagination but they all involved rewriting thepaginateCount
> method for a particular case of pagination, which is not what I want
> (I want cake to keep doing all the magic for me :D ). So, I've been
> playing with the cake lib, and found a way to solve it, however... I
> assume it might have side effects, and would like your opinion on the
> matter, whether this can be implemented in a "cleaner" way or if I'm
> totally off track.
>
> So, in the controller.php find method, I've placed (REplaced,
> actually) this code (Line 1030):
>
>                 if (method_exists($object, 'paginateCount')) {
>                         $count = $object->paginateCount($conditions, $recursive, $extra);
>                 } else {
>                         $parameters = compact('conditions');
>                         if ($recursive != $object->recursive) {
>                                 $parameters['recursive'] = $recursive;
>                         }
>                         //$count = $object->find('count', array_merge($parameters,
> $extra)); //OLD LINE
>                         $count = $object->find('count', $fields); //NEW LINE
>                 }
>
> In my controller, the code for the paginate variable is as follows:
>
>                 var $paginate = array(
>                         'limit' => 25,
>                         'order' => array(
>                                 'User.created' => 'desc'
>                         ),
>                         'joins' => array(
>                                 'LEFT JOIN user_areas AS Areas ON User.id = Areas.user_id',
>                                 'LEFT JOIN user_tools AS Tools ON User.id = Tools.user_id',
>                                 'LEFT JOIN user_careers AS Careers ON User.id = Careers.user_id'
>                         ),
>                         'fields' => array(
>                                 'DISTINCT User.id',
>                                 'User.dni',
>                                 'User.name',
>                                 'User.email',
>                                 'User.password',
>                                 'User.interviewed',
>                                 'User.keywords',
>                                 'User.birthday',
>                                 'User.cv',
>                                 'User.filetype',
>                                 'User.comment',
>                                 'User.province_id',
>                                 'User.town',
>                                 'User.remuneration',
>                                 'User.created',
>                                 'User.modified',
>                                 'User.admin',
>                                 'Province.id',
>                                 'Province.country_id',
>                                 'Province.name',
>                                 'UserSecondary.id',
>                                 'UserSecondary.user_id',
>                                 'UserSecondary.secondary_id',
>                                 'UserSecondary.institute',
>                                 'UserSecondary.status_id'
>                         )
>                 );
>
> So, I would like to hear some thoughts please.
>
> Thanks in advance!
--~--~---------~--~----~------------~-------~--~----~
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: