我需要在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));
如您所见,我将条件数组相互合并,然后将它们发送到paginate。这在CakePHP 2.7中运行良好。但是现在我收到了错误
Column not found: 1054 Unknown column 'contacts.lname' in 'where clause'.
lname
列肯定会在数据库表中退出。有什么我做错了。如果是这样,有人可以告诉我正确的方法进行连续搜索,因为我一直试图这样做。
答案 0 :(得分:2)
Yu必须使用查询表达式,但这不能在分页数组中完成。
所以在这里按照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));