1

How to set scenario in object created from Join Query in Yii2

My query is

$model = Answers::find()
            ->joinWith('question')
            ->where(['request_id' => $Request->id])->all();

I am getting data in model object. Now when i try to set scenario to this object

$model->scenario = 'get_answer';

it is giving error "PHP Warning – yii\base\ErrorException

Attempt to assign property of non-object"

How to set scenario to this object.

Ninja Turtle
  • 1,293
  • 2
  • 24
  • 50
  • Probably `Answers::find()...->all()` returns array of Answers. And after this action your `$model` is array; And yes, you cant assign property to array. Explain us please what do you want with this answers. – Bukharov Sergey Feb 14 '17 at 13:33
  • @BukharovSergey Yes i am getting array of object in $model variable. Now i want to assign scenario 'get_answer' to these objects – Ninja Turtle Feb 14 '17 at 13:43

1 Answers1

0

Answers::find()...->all() returns array of Answers. And after this action your $model is array;

And yes, you cant assign property to array. Explain us please what do you want with this answers.

Your code must be like this:

$answers = Answers::find()
        ->joinWith('question')
        ->where(['request_id' => $Request->id])->all();

foreach ($answers as $answer) {
   $answer->scenario = 'get_answer';
}
Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54