#5123: Incorrect find('count') results when using "GROUP BY" and
Controller::paginate()
------------------------------------------+---------------------------------
    Reporter:  bunyan                     |         Owner:          
        Type:  Enhancement                |        Status:  reopened
    Priority:  Low                        |     Milestone:  2.0.0.x 
   Component:  MySQL                      |       Version:  Alpha   
    Severity:  Normal                     |    Resolution:          
    Keywords:  paginate, count, group by  |   Php_version:  PHP 5   
Cake_version:  1.2.0.7296 RC2             |  
------------------------------------------+---------------------------------
Comment (by sdewald):

 The solution above worked for me but used a significant amount of memory
 when dealing with large result sets (over 100mb in my case, causing my php
 to crash for some queries).  I think the problem is often overlooked since
 the memory is freed before control is returned the user, but a quick check
 of memory_get_peak_usage() will reveal the problem.
 [[BR]]
 [[BR]]

 I think I found a solution that accomplishes the same thing with less
 memory when dealing with one GROUP BY field. Feedback would be much
 appreciated.


 {{{
 public function paginateCount($conditions = null, $recursive = 0, $extra =
 array()) {

     $parameters = compact('conditions', 'recursive');

     if (isset($extra['group'])) {
         $parameters['fields'] = $extra['group'];

         if (is_string($parameters['fields'])) {
             // pagination with single GROUP BY field
             if (substr($parameters['fields'], 0, 9) != 'DISTINCT ') {
                 $parameters['fields'] = 'DISTINCT ' .
 $parameters['fields'];
             }

             unset($extra['group']);

             $count = $this->find('count', array_merge($parameters,
 $extra));
         } else {
             // resort to inefficient method for multiple GROUP BY fields
             $count = $this->find('count', array_merge($parameters,
 $extra));

             $count = $this->getAffectedRows();

         }

     } else {
         // regular pagination
         $count = $this->find('count', array_merge($parameters, $extra));
     }


     //echo '<br />mem: ' . number_format(memory_get_usage(), 0, '.', ',');
     //echo '<br />peak: ' . number_format(memory_get_peak_usage(), 0, '.',
 ',');

     return $count;
 }
 }}}

-- 
Ticket URL: <https://trac.cakephp.org/ticket/5123#comment:10>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design 
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. 
Our primary goal is to provide a structured framework that enables PHP users at 
all levels to rapidly develop robust web applications, without any loss to 
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"tickets 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/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to