N
N
Nikita Adamkevich2015-02-01 06:24:10
Yii
Nikita Adamkevich, 2015-02-01 06:24:10

How in Yii2, if there is no associated model, create an empty one in its place?

There is a model. It has a HAS_ONE relationship.
You need to edit all the fields of the model and its links in one form.
If the data is there, then everything is ok. If we create a new object, then it has no associated data. However, you cannot write $model->children = new Childrens(); as I successfully did in the first yii, since the children variable is read-only.
I don’t really want to write a bunch of wrappers like creating a left variable, writing data from a form into it, a bunch of conditions, and so on and so forth.
Is there an adequate solution or will you still have to fence a bunch of extra code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2015-02-01
@arenyrium

In yii2, association cannot happen if both models don't have a primary key. Therefore, only through the creation of a property of the main model.
You can write a setChildren setter for the main model and use it to write to the $childrenRelation private property of the main model. In afterSave, it's easy to link the models via the link() method. Then in the controller everything should look as simple for you as in yii1, just $model->children = new Childrens();.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question