I have a question how to use the Closure type Active records Query in YII2 with conditional WHERE.
Here is what I want to achieve:
public function getUsers($limit = 10, $type = 1, $company_id = 0) {
return User::find()->where( function($query) use ($type, $company_id){
$query->where(['type' => $type]);
if($company_id != 0) {
$query->andWhere(['company_id' => $company_id]);
}
})
->orderBy([ 'created_at'=> SORT_DESC, ])
->limit($limit);
}
Please help if anyone knows about this.
source
share