简体   繁体   English

在CakePHP中的WHERE IN中使用数组

[英]using array in WHERE IN in CakePHP

I'm working on CakePHP 3.2 我正在开发CakePHP 3.2

I want to use an array in the query WHERE IN () 我想在查询WHERE IN ()使用数组

The code is as 代码是

$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',
 ]);

Here $filter_p_t is an array from url with parameter 这里$filter_p_t是来自带参数的url的数组

/products/display/1?p_t[]=1&p_t[]=86

I want to include $filter_p_t in to the where condition to find all IN(1,86) 我想将$filter_p_t包含在where条件中以查找所有IN(1,86)

How can I use php array to the WHERE IN condition in CakePHP 3? 如何在CakePHP 3中使用php数组到WHERE IN条件?

You can use the IN keyword directly after the column. 您可以在列之后直接使用IN关键字。 This is assuming that $filter_p_t is an array like array(1, 86) . 这假设$filter_p_t是一个类似array(1, 86) $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 http://book.cakephp.org/3.0/en/orm/query-builder.html#automatically-creating-in-clauses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM