cakephp 和 SQL_CALC_FOUND_ROWS
我正在尝试将 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
应用到表,例如 SELECT
Category。< /code>SQL_CALC_FOUND_ROWS
,
有没有办法做到这一点?任何帮助将不胜感激,谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想查看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".也许你可以将你的字段参数设置如下:
Maybe you can make your field parameter as below:
这是一个可怕的,可怕的黑客攻击,将未转义的
SQL_CALC_FOUND_ROWS
放入查询中,但它有效:所有功劳都归于来自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:All credit goes to "Kani" from http://mogura.in/blog/2011/06/17/cakephp-1-3-sql_calc_found_rows.
我找到了一种用cake内置函数来实现的方法。
I found a way to realize it with cake built in functions.