20

我正在尝试使用 CakePHP paginate() 方法在 SQL 查询中使用“HAVING”子句。

经过一番搜索,这似乎无法通过 Cake 的 paginate()/find() 方法实现。

我的代码看起来像这样:

$this->paginate = array(
        'fields' => $fields,
        'conditions' => $conditions,
        'recursive' => 1,
        'limit' => 10, 
        'order' => $order,
        'group' => 'Venue.id');

$fields 之一是别名“距离”。我想添加一个查询何时距离 < 25(例如 HAVING 距离 < 25)。

到目前为止,我已经看到了两种解决方法,不幸的是,它们都不适合我的需要。我见过的两个是:

1)在“组”选项中添加HAVING子句。例如'group' => 'Venue.id HAVING distance < 25'。当与分页结合使用时,这似乎不起作用,因为它会弄乱执行的初始计数查询。(即尝试使用SELECT distinct(Venue.id HAVING distance < 25)明显无效的语法。

2) 在 WHERE 条件之后添加 HAVING 子句(例如WHERE 1 = 1 HAVING field > 25) 这不起作用,因为看起来 HAVING 子句必须出现组语句之后,Cake 在它生成的查询中放置在 WHERE 条件之后。

有谁知道使用 CakePHP 的 find() 方法可以做到这一点?我不想使用 query() ,因为这会涉及大量返工,也意味着我需要实现自己的分页逻辑!

提前致谢

4

6 回答 6

36

您必须将其与组条件放在一起。像这样

$this->find('all', array(
    'conditions' => array(
        'Post.length >=' => 100
    ),
    'fields' => array(
        'Author.id', 'COUNT(*) as Total'
    ),
    'group' => array(
        'Total HAVING Total > 10'
    )
));

希望对你有帮助

于 2011-10-13T12:48:32.693 回答
1

我使用以下技巧在 WHERE 子句的末尾添加了自己的 HAVING 子句。蛋糕子查询文档中提到了“dbo->expression()”方法。

function addHaving(array $existingConditions, $havingClause) {
  $model = 'User';
  $db = $this->$model->getDataSource();

  // Two fun things at play here,
  // 1 - mysql doesn't allow you to use aliases in WHERE clause
  // 2 - Cake doesn't allow a HAVING clause separate from a GROUP BY
  // This expression should go last in the WHERE clause (following the last AND)
  $taut = count($existingConditions) > 0 ? '1 = 1' : '';
  $having = $db->expression("$taut HAVING $havingClause");

  $existingConditions[] = $having;

  return $existingConditions;
}
于 2012-05-29T04:38:34.723 回答
1

As per the manual, CakePHP/2 supports having at last. It was added as find array parameter on version 2.10.0, released on 22nd July 2017.

From the 2.10 Migration Guide:

Model::find() now supports having and lock options that enable you to add HAVING and FOR UPDATE locking clauses to your find operations.

于 2018-10-03T11:45:45.307 回答
0

刚遇到同样的问题。我知道,不应该修改内部代码,但如果你打开PaginatorComponent并修改第 188 行:

$count = $object->find('count', array_merge($parameters, $extra));

对此:

$count = $object->find(
             'count', 
             array_merge(array("fields" => $fields),$parameters, $extra)
         );

一切都会得到解决。您将能够将 HAVING 子句添加到“组”中,并且 COUNT(*) 不会有问题。

或者,制作一行:

$count = $object->paginateCount($conditions, $recursive, $extra);

包括$fields

$count = $object->paginateCount($fields,$conditions, $recursive, $extra);

之后,您可以“覆盖”模型上的方法,并确保在 find() 中包含 $fields,就是这样!,=P

于 2012-05-20T04:38:08.610 回答
0

这是另一个不能解决分页问题的想法,但它很干净,因为它只是覆盖了 AppModel 中的 find 命令。只需在查询中添加一个组和具有元素,这将转换为 HAVING 子句。

public function find($type = 'first', $query = array()) {
    if (!empty($query['having']) && is_array($query['having']) && !empty($query['group'])) {
      if ($type == 'all') {
        if (!is_array($query['group'])) {
          $query['group'] = array($query['group']);
        }

        $ds = $this->getDataSource();
        $having = $ds->conditions($query['having'], true, false);
        $query['group'][count($query['group']) - 1] .= " HAVING $having";

        CakeLog::write('debug', 'Model->find: out query=' . print_r($query, true));
      } else {
        unset($query['having']);
      }
    }

    return parent::find($type, $query);
  }

在这里找到

https://groups.google.com/forum/?fromgroups=#!topic/tickets-cakephp/EYFxihwb55I

于 2012-10-29T18:40:12.113 回答
0

在 find 中使用“有”对我不起作用。相反,我将组“ group => product_id, color_id 的 sum(quantity) > 2000 ”放入一个字符串中,就像一个魅力。使用 CakePHP 2.9

于 2021-01-03T17:34:46.143 回答