Reputation: 735
I have this bit of code which finds the ID of the logged in user, finds all of the customers that are related to that user and stores the 'custref'(id) into an array:
$chan = Yii::$app->user->identity->typedIdentity->getId();
$cust = Customer::find()->where(['channelref' => $chan])->all();
foreach($cust as $row){
$ref[] = $row->custref;
}
I want to use the values in $ref inside of a query, something like this:
$query = $model::findNongeo()->where(['custref'=>$ref->custref])->all();
But I have no idea how to do it, I have tried all sorts. Any help would be massively appreciated.
Upvotes: 0
Views: 2205
Reputation: 9358
$ref = Customer::find()
->select('custref')
->where(['channelref' => Yii::$app->user->identity->typedIdentity->getId()])
->column();
$query = $model::findNongeo()
->where(['custref' => $ref])
->all();
Upvotes: 1
Reputation: 1184
Here is the shortest way.
$chanId = Yii::$app->user->identity->typedIdentity->getId();
$customerArray = Customer::find()->where(['channelref' => $chanId])->all();
$ref = yii\helpers\ArrayHelper::map($customerArray,'custref','custref');
You can use in other query like
$query = $model::findNongeo()->where(['custref'=>$ref])->all();
OR Like
$query = $model::findNongeo()->where(['in', 'custref', $ref])->all();
Upvotes: 3
Reputation: 198
You are going right. Just change the code in loop like
$i = 0;
foreach($cust as $row){
$ref[$i++] = $row->custref;
}
Upvotes: 1
Reputation: 2499
You can use the values in the array by using In
$model::find()->where(['in', 'custref', $ref])->all();
Upvotes: 1