[英]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. 我正在使用yii2
Advanced。
I have 3 tables 我有3张桌子
Third table - Business_tbl(business.id, business.name) 第三张表-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. 在您的代码中= $ UID应该是一个数组,否则它将始终只有一个id。 the last in your $businessUsers. $ 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. 您可能希望将所有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,
]
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.