Skip to main content

Posts

Showing posts from 2009

CakePHP: pagination & sorting on deep associated models (for custom behaviors like Linkable or virtual / aggregated fields)

Using in CakePHP custom behavior (like LinkableBehavior ) or aggregated fields (sql count, max, min , etc queries) or virtual fields in model breaks sorting with PaginationHelper . Trying to sort on field that does not explicitly defined in the model or directly associated models leads to loosing all sorting information. To fix this you need to correct Controller->paginate() method. To do so create file app/app_controller.php if it doesn't exist and define AppController class there. Add paginate() method to it and copy it's content from the same method from cake/libs/controller/controller.php file. After that replace $value = $options['order'][$key]; unset($options['order'][$key]); if (isset($object->{$alias}) && $object->{$alias}->hasField($field)) { $options['order'][$alias . '.' . $field] = $value; } elseif ($object->hasField($field)) { $options['order'][$alias . '.' . $field] = $value; } with $v