Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 810
Anuj TBE Avatar asked Oct 14 '16 17:10

Anuj TBE


People also ask

How can I get data from database in cakephp?

To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.

How can I order in cakephp?

To apply ordering, you can use the order method: $query = $articles->find() ->order(['title' => 'ASC', 'id' => 'ASC']); When calling order() multiple times on a query, multiple clauses will be appended.

What is Set in CakePHP?

compact() or set() in cakephp set() is the way to set values in your controller and get those values in your view file. Syntax of set is-> $this->set('variable','value')


1 Answers

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

like image 121
bill Avatar answered Oct 05 '22 11:10

bill