closures - Multiple where clause in YII2 -


i have question, how use closure type active records query in yii2 conditional where.

here want achive:

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 if know this.1

can't figure out closure here. can use andfilterwhere() company_id condition, should set null default, condition ignored if company_id wasn't initialized:

public function getusers($limit = 10, $type = 1, $company_id = null) {      return user::find()             ->where(['type' => $type])             ->andfilterwhere(['company_id' => $company_id])             ->orderby([ 'created_at'=> sort_desc ])              ->limit($limit)             ->all(); //probably muiss } 

http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#andfilterwhere()-detail


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -