Open e-frank opened 10 years ago
How do you expect the entry in the relation table to be created when the model has no primaryKey value assigned?
sure you are right. i wished i found some automagic here and expected a 'linked save' to save the complete model-tree. [or the same behaviour with and without ->viaTable
. with viaTable the child model is saved, without isn't.]
when you use ->link
a second time, you will get an additional row in the relation table, and no error, if you don't have an unique index.
i didn't find anything like a isLinked
method, so what i think i have to do is test the parent relationship for an existing child by the child's primary key, like:
$hasChild = $parent->getRelation($relation)->andWhere('id=1')->exists();
and then decide if you need ->link
.
i am working on a hydrator and hope to publish it asap.
still: fantastic work!
closed in favor of https://github.com/yiisoft/yii2/issues/4834
@dynasource this issue is #4834 :)
:), if this was an algorithm, all the issues would have been closed at once
usually i link 1:n models with its "link" method. if i use a pivot table, i have to use on more "save" command on the related model. i am not sure if this is a bug or a feature, but i would find it more conclusive to avoid the additonal save. here the example scenario: setup db, creates tables "test", "testline" and "test_testline" (attention: drop table) :
Test-Model with two different relationships, doing almost the same thing:
Testline Model:
in a test view: