E
E
EVOSandru62015-10-10 05:03:21
Yii
EVOSandru6, 2015-10-10 05:03:21

Why doesn't Yii guard linked models like in the recipe?

Good afternoon,
I wanted to do it like here yiiframework.ru/doc/cookbook/ru/model.save.related.data in the documentation.
There is a Hotels model and a rooms relation in this model:
'rooms '=>[self::HAS_MANY, 'Rooms', 'hotel_id']
I submit the form, $_POST prints out all the data, but no data is entered into the rooms relation , an example is given below.
Perhaps the documentation meant not a relation, but an array?

if(isset($_POST['Hotels']))
    {
      $model->setAttributes($_POST['Hotels'], true);
      $arr = [];
      // $model->rooms= [];   так тоже ничего не заносится, если как массив объявить

      foreach($_POST['Rooms'] as $room)
      {
        $roomModel = new Rooms();
        $roomModel->setAttributes($room, true);
        $model->rooms[] = $roomModel;
        $arr[] = $roomModel;
      }

                        // ничего не выводит
      My::printArr($model->rooms);
                       // Выводит массив моделей
      My::printArr($arr);

      $model->save();
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-10-10
@bIbI4k0

Try like this

foreach($_POST['Rooms'] as $room)
      {
        $roomModel = new Rooms();
        $roomModel->setAttributes($room, true);
        $roomModel->hotel = $model;
        $roomModel->save();
      }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question