A
A
Arnold2015-12-08 16:46:24
Yii
Arnold, 2015-12-08 16:46:24

Yii2 many-to-many how to save correctly?

I’ll start not like everyone else) I don’t have three tables, but two.
locations

  • *id
  • name

locations_parents
  • *id
  • parent_id
  • child_id
  • main

Each entry in a table can be both a parent and a child of entries in the same table.
Accordingly, there can be many daughters and many parents. The main field specifies the main parent.
Actually, such a structure provides an infinite tree.
In Yii2, I try to transfer and save the list of parents of the edited object through the droplist widget with the multiplay into the model.
Performance:
$form->field($model, 'parents_ids')->dropDownList($model->getListLocality(), ['class' => 'chzn-select', 'multiple' => true])

Model:
class Locality extends ActiveRecord
{
    const PUBLISH = 1;
    const UNPUBLISHED = 0;
    public $parents_ids = array();
...
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            print_r($this->parents_ids);
            die();
            return true;
        } else {
            return false;
        }
    }
}

But $this->parents_ids is empty. I really don't want to use something like \Yii::$app->request->post('parents_ids')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korovin, 2015-12-09
@Yii2SlowMoooo

Good afternoon. What did you write in the rules for this property?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question