0

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.

asked Nov 8, 2017 at 16:14

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

You are going right. Just change the code in loop like

$i = 0;
foreach($cust as $row){
 $ref[$i++] = $row->custref;
}
answered Nov 8, 2017 at 18:08

Comments

1
$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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.