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.

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();
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();

Comments

1

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

$i = 0;
foreach($cust as $row){
        $ref[$i++] = $row->custref;

}

Comments

1
$ref = Customer::find()
    ->select('custref')
    ->where(['channelref' => Yii::$app->user->identity->typedIdentity->getId()])
    ->column();

$query = $model::findNongeo()
    ->where(['custref' => $ref])
    ->all();

Comments

Your Answer

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.