在CakePHP中的WHERE IN中使用数组

时间:2016-10-14 17:34:32

标签: cakephp where-in cakephp-3.2

我正在使用CakePHP 3.2

我想在查询WHERE IN ()

中使用数组

代码为

$filter_p_t = $this->request->query('p_t');

$pros10 = $this->Products->find()
 ->where([
   'SellerProducts.stock >' => 0,
   'SellerProducts.status' => 1,
   'Products.p_status' => 1,
 ])
 ->contain([
   'SellerProducts',
   'ProductTypes.Subcategories.Categories',
 ]);

此处$filter_p_t是来自带参数

的网址的数组
/products/display/1?p_t[]=1&p_t[]=86

我想在$filter_p_t条件中添加where以查找所有IN(1,86)

如何将Cake数组用于CakePHP 3中的WHERE IN条件?

1 个答案:

答案 0 :(得分:8)

您可以在列之后直接使用IN关键字。这假设$filter_p_t是一个类似array(1, 86)的数组。

->where(['id IN' => $filter_p_t]);

http://book.cakephp.org/3.0/en/orm/query-builder.html#automatically-creating-in-clauses