【问题标题】:Search with concat fields in cakephp 3在 cakephp 3 中使用 concat 字段进行搜索
【发布时间】:2016-02-15 09:09:56
【问题描述】:

我需要在 CakePHP 3 中使用 $this->Paginate 进行搜索查询。以下是我正在使用的代码

$searchCondition = array(
    'OR' => array(
        'Quotes.quotenum LIKE' => "%" . $this->request->data['Quote']['keyword'] . "%",
        'Branches.name LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
        'Contacts.fname LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
        'Contacts.lname LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
        'CONCAT(Contacts.fname, Contacts.lname) LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
        'Quotes.description LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%'
    )
);

$cond = array(
    'conditions' => array_merge($searchConditions, $searchCondition, $limo),
    'order'= > array('Quotes.quotenum desc'),
    'contain' => array('Branches','Contacts')
);

$this->set('articleList', $this->paginate($this->Quotes));

如您所见,我将条件数组相互合并,然后将它们发送到分页。这在 CakePHP 2.7 中运行良好。但是现在我得到了错误

Column not found: 1054 Unknown column 'contacts.lname' in 'where clause'.

lname 列肯定存在于数据库表中。有什么我做错了吗。如果是这样,有人可以告诉我进行 concat 搜索的正确方法,因为我一直在尝试这样做。

【问题讨论】:

  • 您没有在代码示例中的任何地方使用$cond 变量。另外为什么别名不同,在错误消息中它是小写的,在您的代码 sn-p 中它以大写字母开头?
  • @ndm 似乎是小写别名的蛋糕。我测试了一个类似的代码,我得到了同样的错误。我尝试的代码只不过是$table->find()->where(['CONCAT(Users.name, Users.family_name) LIKE' => '%test%']);
  • @arilia 啊,我忽略了第二个lname 参考...您需要改用查询表达式。
  • @ndm 是的,但似乎他正在创建分页数组。所以也许实际的问题可能是:如何在分页数组中使用查询表达式?
  • @arilia 答案是你不能,你必须使用自定义查找器或传递查询对象。

标签: cakephp cakephp-3.0 query-builder


【解决方案1】:

你必须使用查询表达式,但这不能在分页数组中完成。

所以按照 ndn 的建议,我会这样做

创建自定义查找器。在您的 QuotesTable 文件中

public function findByKeyword(Query $query, array $options)
{
    $keyword = $options['keyword'];
    $query->where(
        function ($exp, $q) use($keyword){
            $conc = $q->func()->concat([
                'Contacts.fname' => 'literal', ù
                'Contacts.lname' => 'literal']);
            return $exp
                ->or_([
                    'Quotes.quotenum LIKE' => "%$keyword%",
                    'Branches.name LIKE' => "%$keyword%",
                    'Contacts.fname LIKE' => "%$keyword%",
                    'Contacts.lname LIKE' => "%$keyword%",
                    'Quotes.description LIKE' => "%$keyword%"
                ])
                ->like($conc, "%$keyword%");
            }
        );
    return $query;
}

然后在你的控制器中

$this->paginate = [
        'finder' => [
            'byKeyword' => [
                'keyword' => $this->request->data['Quote']['keyword']
        ]],
        'conditions' => $limo,  // this will merge your $limo conditions                  
                                // with the ones you set in the custom finder
        'order'= > ['Quotes.quotenum desc'],
        'contain' => ['Branches','Contacts']
    ];

$this->set('articleList', $this->paginate($this->Quotes));

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多