cakephp 和 SQL_CALC_FOUND_ROWS

发布于 2024-09-03 19:58:58 字数 971 浏览 9 评论 0 原文

我正在尝试将 SQL_CALC_FOUND_ROWS 添加到查询中(请注意,这不是用于分页)

请注意,我正在尝试将其添加到 cakePHP 查询中,我当前拥有的代码如下:

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')))
            ));

注意 'fields'=> ;array('SQL_CALC_FOUND_ROWS',' 这显然不起作用,因为它尝试将 SQL_CALC_FOUND_ROWS 应用到表,例如 SELECTCategory。< /code>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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁〃客ぐ 2024-09-10 19:58:58

您可能想查看cakephp使用mysql SQL_CALC_FOUND_ROWS进行分页。这个人的语法与你相似,这对他有用。

如果这没有帮助,您可以随时使用 $this->find('count', $params); (http://book.cakephp.org/view/1020/find-count) 或 $this->query('您的 SQL 查询这里'); (http://book.cakephp.org/view/1027 /查询)。

除此之外,您不应将 'joins''contain' 一起使用。根据文档,“可能会导致一些 SQL 错误(重复的表),因此您需要使用 joins 方法作为 Containable 的替代方法”。

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".

野侃 2024-09-10 19:58:58

也许你可以将你的字段参数设置如下:

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

Maybe you can make your field parameter as below:

'fields'=>array('SQL_CALC_FOUND_ROWS *','COUNT(`Entity`.`id`) as `entity_count`')
恰似旧人归 2024-09-10 19:58:58

这是一个可怕的,可怕的黑客攻击,将未转义的SQL_CALC_FOUND_ROWS放入查询中,但它有效:

$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`');

所有功劳都归于来自http://mogura.in/blog/2011/06/17/cakephp-1-3-sql_calc_found_rows

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.

蒗幽 2024-09-10 19:58:58

我找到了一种用cake内置函数来实现的方法。

$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()']);

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()']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文