【发布时间】: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