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.
4 Answers 4
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();
answered Nov 9, 2017 at 4:27
Naim Malek
1,1841 gold badge11 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can use the values in the array by using In
$model::find()->where(['in', 'custref', $ref])->all();
answered Nov 8, 2017 at 17:28
mrateb
2,5196 gold badges32 silver badges64 bronze badges
Comments
You are going right. Just change the code in loop like
$i = 0;
foreach($cust as $row){
$ref[$i++] = $row->custref;
}
Comments
$ref = Customer::find()
->select('custref')
->where(['channelref' => Yii::$app->user->identity->typedIdentity->getId()])
->column();
$query = $model::findNongeo()
->where(['custref' => $ref])
->all();
answered Nov 9, 2017 at 8:20
Insane Skull
9,3619 gold badges49 silver badges66 bronze badges
Comments
default