Reputation: 853
I am using Yii 2.0 PHP framework. Is it possible to put where clauses inside a where clause? In my case, I want to put orwhere
clauses inside andwhere
:
$employees = Employee::find()
->where([ 'parent' => new \MongoId($session['company_owner']) ])
->andwhere([ Employee::find()
->orwhere([ 'employee_id' => new \MongoRegex("/".$keyword."/i") ])
->orwhere([ 'department_name' => new \MongoId($departments['_id']) ])
->orwhere([ 'division_name' => new \MongoId($divisions['_id']) ])
->orwhere([ 'job_title' => new \MongoId($jobpositions['_id']) ])
])->all();
The code above returns this error:
It's because it's not the correct syntax/structure, I think. Anyway, here's my original code:
$employees = Employee::find()
->orwhere([ 'employee_id' => new \MongoRegex("/".$keyword."/i") ])
->orwhere([ 'department_name' => new \MongoId($departments['_id']) ])
->orwhere([ 'division_name' => new \MongoId($divisions['_id']) ])
->orwhere([ 'job_title' => new \MongoId($jobpositions['_id']) ])
->andwhere([ 'parent' => new \MongoId($session['company_owner']) ])
->all();
It's working fine, but I want to restructure it since it takes time. It still has to go through that series of orwhere
's before getting to andwhere
. That's why I tried restructuring the query but it's not working.
Do you have any idea on how to do it?
Upvotes: 1
Views: 103
Reputation: 25312
You should try this :
$employees = Employee::find()
->where([ 'parent' => new \MongoId($session['company_owner']) ])
->andwhere([
'or',
[ 'employee_id' => new \MongoRegex("/".$keyword."/i") ],
[ 'department_name' => new \MongoId($departments['_id']) ],
[ 'division_name' => new \MongoId($divisions['_id']) ],
[ 'job_title' => new \MongoId($jobpositions['_id']) ],
])->all();
Read more : http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail
Upvotes: 2