在foreach循环内搜索模型的Yii2查询

时间:2017-11-02 10:57:37

标签: php yii2

这是我的代码,但只输出最后一个用户。我的意思是它不会显示注册到特定企业的其他用户。用户应该只能看到在其业务中注册的用户。提前致谢

我正在使用yii2高级版。

我有3张桌子

  • 第一个表 - user_tbl(user.id,user.username,user.email)
  • 第二个表 - userinfo_tbl(info.id,info.userid,info.business id)
  • 第三个表 - 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,
                ]
            ]);
    

3 个答案:

答案 0 :(得分:0)

在你的代码中= $ UID应该是一个数组,否则它将始终只有一个id。 $ businessUsers中的最后一个。 将您的代码更改为:

    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,
        ]
    ]);
}

答案 1 :(得分:0)

试试这个简单的方法

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,
        ]
    ]);

答案 2 :(得分:0)

您可能希望将所有ID放在逗号分隔列表中,然后在where条件中使用IN查询。

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,
            ]
        ]);