开发者

cakephp and SQL_CALC_FOUND_ROWS

I am trying to add the SQL_CALC_FOUND_ROWS into a query (Please note this isn't for pagination)

please note I am trying to add this to a cakePHP query the code I currently have is below:

return $this->find('all', array(
                'conditions' => $conditions,
                'fields'=>array('SQL_CALC_FOUND_ROWS','Category.*','开发者_开发百科COUNT(`Entity`.`id`) as `entity_count`'),
                'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
                'group' => '`Category`.`id`',
                'order' => $sort,
                'limit'=>$params['limit'],
                'offset'=>$params['start'],
                'contain' => array('Domain' => array('fields' => array('title')))
            ));

Note the 'fields'=>array('SQL_CALC_FOUND_ROWS',' this obviously doesn't work as It tries to apply the SQL_CALC_FOUND_ROWS to the table e.g. SELECTCategory.SQL_CALC_FOUND_ROWS,

Is there anyway of doing this? Any help would be greatly appreciated, thanks.


You may want to look at cakephp paginate using mysql SQL_CALC_FOUND_ROWS. The person had similar syntax as you have and it worked for him.

If that doesn't help you can always use $this->find('count', $params); (http://book.cakephp.org/view/1020/find-count) or $this->query('YOUR SQL QUERY HERE'); (http://book.cakephp.org/view/1027/query).

Besides that you should not use 'joins' together with 'contain'. According to the documentation that "could lead to some SQL errors (duplicate tables), so you need to use the joins method as an alternative for Containable".


Maybe you can make your field parameter as below:

'fields'=>array('SQL_CALC_FOUND_ROWS *','COUNT(`Entity`.`id`) as `entity_count`')


This is a horrible, horrible hack to get an unescaped SQL_CALC_FOUND_ROWS into the query, but it works:

$categories = $this->Category->find('all', array(
   'fields' => array('SQL_CALC_FOUND_ROWS 0.0 AS dummy_field,1', 'Category.*', ...),
   'limit'  => 42,
   ...
));
$totalCategories = $this->Category->query('SELECT FOUND_ROWS() as `total_categories`');

All credit goes to "Kani" from http://mogura.in/blog/2011/06/17/cakephp-1-3-sql_calc_found_rows.


I found a way to realize it with cake built in functions.

$dbo = $this->User->getDataSource();

//buildStatement() creates a Standard SQL Statement
$subQuery = $dbo->buildStatement(
    array(
        'fields' => $fields,
        'table' => $dbo->fullTableName($this->User),
        'alias' => 'User',
        'limit' => null,
        'offset' => null,
        'joins' => array(),
        'conditions' => $conditions,
        'order' => null,
        'group' => null
    ),
    $this->User
);

//Add the SQL_CALC_FOUND_ROWS part
$subQuery = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $subQuery);
$Users = $this->User->query($subQuery);

//Get FOUND ROWS
$foundRows = $this->User->query("SELECT FOUND_ROWS()");
$count = intval($foundRows[0][0]['FOUND_ROWS()']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜