简体   繁体   中英

using array in WHERE IN in CakePHP

I'm working on CakePHP 3.2

I want to use an array in the query 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

/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)

How can I use php array to the WHERE IN condition in CakePHP 3?

You can use the IN keyword directly after the column. This is assuming that $filter_p_t is an array like array(1, 86) .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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