在cakephp 3中使用concat字段进行搜索 [英] Search with concat fields in cakephp 3

查看:51
本文介绍了在cakephp 3中使用concat字段进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to make a search query using $this->Paginate in CakePHP 3. Following is the code I am using

$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中很好用.但是现在我得到了错误

As you can see I merge the condition arrays with each other and then send them to paginate. This worked fine in CakePHP 2.7. However now I get the error

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

lname 列肯定在数据库表中退出.我做错了什么吗?如果是这样,有人可以告诉我正确的方法进行concat搜索,因为我已经尝试了很长时间了.

The lname column definitely exits in the database table. Is there something I am doing wrong. If so, could someone tell me the right way to do concat search as I have been trying to do this for quite some time.

推荐答案

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

Yu have to use query expression but this can't be done in a pagination array.

因此,在ndn建议之后,这就是我的做法

So Following ndn suggestion here's how I would do

创建一个自定义查找器.在您的QuotesTable文件中

create a custom finder. In your QuotesTable file

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

这篇关于在cakephp 3中使用concat字段进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆