Skip to content

Instantly share code, notes, and snippets.

@interlock
Created March 26, 2012 16:02
Show Gist options
  • Select an option

  • Save interlock/2206161 to your computer and use it in GitHub Desktop.

Select an option

Save interlock/2206161 to your computer and use it in GitHub Desktop.
virtual fields, pagination with conditions on those virtual fields
CakePHP Version: 1.3.14
Problem:
Warning (512): SQL Error: 1054: Unknown column 'UserProfile.full_name' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: SELECT COUNT(*) AS `count` FROM `users` AS `User` LEFT JOIN `user_profiles` AS `UserProfile` ON (`UserProfile`.`user_id` = `User`.`id`) WHERE `UserProfile`.`full_name` LIKE '%sss%' AND `User`.`deleted` != 1
Expected Result:
countQuery to include virtual fields to conditions based on virtual fields can be executed without error.
Actual Result:
Since the virtual field is not defined, conditions use for pagination on virtual fields will fail.
Regression:
Looks like the paginationCount function just assumes you will only ever use conditions on real fields. Manual states you can use virtual fields like normal, but clearly only on demand. Since the pagination counter function just does a COUNT(*) it doesn't effectively catch cases where where sorts or conditions could be on a virtual field. A common case IMHO.
<?php
class Profile extend AppModel {
var $belongsTo = array('User');
var $virtualFields = array(
'full_name' => 'CONCAT(`Profile`.`first_name`, ' ', `Profile`.`last_name`)'
);
?>
<?php
class User extends AppModel {
var $actsAs = array('Containable');
var $hasOne = array('Profile');
?>
<?php
class UsersController extends AppController {
// ... regular stuff here
var $paginate = array(
'User' => array(
'limit' => 10,
'contain' => array('Profile')
)
);
function search() {
// hard coded search query
$conditions['Profile.full_name LIKE'] = '%user%';
$this->set('users',$this->paginate('User',$conditions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment