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