M
M
Mikha Pankratov2015-12-15 10:28:10
Yii
Mikha Pankratov, 2015-12-15 10:28:10

How to add more data to an object?

Good afternoon,
I need to get such an array

[
    0 => [
        1 => app\models\UserModel#1
        (
            [yii\db\BaseActiveRecord:_attributes] => [
                'id' => '2199'
                'created' => '2015-12-14 13:12:51'
                'modified' => '2015-12-14 13:12:51'
                'days' => '1'
                'xxx_id' => [
                    0 => 19
                    1 => 20
                ]
                'is_active' => 1
            ]
            [yii\db\BaseActiveRecord:_oldAttributes] => [
                'id' => '2199'
                'created' => '2015-12-14 13:12:51'
                'modified' => '2015-12-14 13:12:51'
                'days' => '1'
                'xxx_id' => '20'
                'is_active' => 1
            ]
            [yii\db\BaseActiveRecord:_related] => []
            [yii\base\Model:_errors] => null
            [yii\base\Model:_validators] => null
            [yii\base\Model:_scenario] => 'default'
            [yii\base\Component:_events] => []
            [yii\base\Component:_behaviors] => []
        )
        2 => app\models\UserModel#2
        (
            [yii\db\BaseActiveRecord:_attributes] => [
                'id' => '2183'
                'created' => '2015-12-14 13:12:51'
                'modified' => '2015-12-14 13:12:51'
                'days' => '2'
                'xxx_id' => '1'
                'is_active' => 1
            ]
            [yii\db\BaseActiveRecord:_oldAttributes] => [
                'id' => '2183'
                'created' => '2015-12-14 13:12:51'
                'modified' => '2015-12-14 13:12:51'
                'days' => '2'
                'xxx_id' => '1'
                'is_active' => 1
            ]
            [yii\db\BaseActiveRecord:_related] => []
            [yii\base\Model:_errors] => null
            [yii\base\Model:_validators] => null
            [yii\base\Model:_scenario] => 'default'
            [yii\base\Component:_events] => []
            [yii\base\Component:_behaviors] => []
        )
        3 => app\models\UserModel#3
        (....)

How to do it in foreach
$models = UserModel::findAll(['user_id' =>  \Yii::$app->user->identity->getId()]);
        foreach ($models as $key => $value) {
            $model[$value->days] = $value;
//            $model[$value->days]->xxx_id[] = $value->xxx_id; - так не пашет
            }
//        $model[1]->availability_id = ['19', '20'];  -- мне надо вот так добавить только в фориче по каждому

// $model[1]->availability_id = ['19', '20']; -- I need to add like this only in the forich for each

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korovin, 2015-12-15
@frmax

You iterate over an array of $models objects, and in a loop you assign a value to some $model, i.e. $model is not defined at all. This is the first. Secondly, if the found records are needed not in the form of objects of the UserModel class, but in the form of arrays, use asArray () (please google it yourself), and of course it would not be bad to make sure that the elements were found at all before using foreach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question