简体   繁体   中英

Yii2 query of search model inside foreach loop

This is my code but it outputs only last user inserted. I mean that it do not display others users registered to a particular business. The user should only see the users that are registered with his business. Thanks in advance

I am using yii2 advanced.

I have 3 tables

  • First table - user_tbl (user.id, user.username, user.email)
  • Second table - userinfo_tbl(info.id, info.userid, info.business id)
  • Third table - Business_tbl(business.id, business.name)

      public function search($params) { //current user logedin $CurrentUserID = Yii::$app->user->getId(); //Find business to which current user is registered $BusinessID = Yii::$app->mycomponent->getBusinessID($CurrentUserID); //Now find all the users with this business id $BusinessUsers= Yii::$app->mycomponent->getBusinessUsersID($BusinessID); foreach ($BusinessUsers as $BusinessUser) { $UID=$BusinessUser['user_id']; } $query = User:: find()->where(['id'=>$UID]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]], 'pagination' => [ 'pageSize' => $this->_pageSize, ] ]); 

In your code = $UID should be an array, otherwise it will always have a single id. the last in your $businessUsers. change your code to this:

    public function search($params)
{
 //current user logedin
     $CurrentUserID = Yii::$app->user->getId();

//Find business to which current user is registered
     $BusinessID = Yii::$app->mycomponent->getBusinessID($CurrentUserID);

//Now find all the users with this business id
    $BusinessUsers=  Yii::$app->mycomponent->getBusinessUsersID($BusinessID);

    //check if $BusinessUsers has records...
    $UID = yii\helpers\ArrayHelper::getColumn($BusinessUsers, 'user_id');
    $query = User:: find()->where(['in','id', $UID]); 
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
        'pagination' => [
            'pageSize' => $this->_pageSize,
        ]
    ]);
}

Try this simple way

public function search($params)
{
    //current user logedin
    $CurrentUserID = Yii::$app->user->getId();

    //Find business to which current user is registered
    $BusinessID = Yii::$app->mycomponent->getBusinessID($CurrentUserID);

    //Now find all the users with this business id
    $BusinessUsers=  Yii::$app->mycomponent->getBusinessUsersID($BusinessID);
    $UID = yii\helpers\ArrayHelper::map($BusinessUsers,'user_id','user_id');

    $query = User:: find()->where(['id'=>$UID]); 
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
        'pagination' => [
            'pageSize' => $this->_pageSize,
        ]
    ]);

You might want to put all the ID's in a comma delimited list then use an IN query in your where condition.

public function search($params)
    {
     //current user logedin
         $CurrentUserID = Yii::$app->user->getId();

    //Find business to which current user is registered
         $BusinessID = Yii::$app->mycomponent->getBusinessID($CurrentUserID);

    //Now find all the users with this business id
        $BusinessUsers=  Yii::$app->mycomponent->getBusinessUsersID($BusinessID);

         foreach ($BusinessUsers as $BusinessUser)
             {
             array_push($UID,$BusinessUser['user_id']); //put all IDs in an array
             }
        $UID = implode(',',$UID); //making it look like this 1,2,3,4
        $query = User:: find()->where('id IN ('.$UID.')'); 

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
            'pagination' => [
                'pageSize' => $this->_pageSize,
            ]
        ]);

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