la-haute-societe/yii2-save-relations-behavior

Trying to set ID of parent model to null

Closed this issue · 2 comments

Hello!

https://monosnap.com/file/BV4PyFdUKuOLvhoj6K3a06wNKcqpPS

I have a problem with saving parent model. When I specifying relation on line SaveRelationBehavior:253 behavior replace primary key of main model and set foreign key of related model.
foreach ($relation->link as $relatedAttribute => $modelAttribute) { if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute}) { $model->{$modelAttribute} = $model->{$relationName}->{$relatedAttribute}; } }

but I have related attribute value is NULL and it's breaking saving process;

I think, solution is check NOT EMPTY related attribute ($model->{$relationName}->{$relatedAttribute})

if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute} && !empty($model->{$relationName}->{$relatedAttribute})) {

juban commented

Hi,

What’s breaking saving process is that your id field is not meant to store null value at a database definition level. Also, setting a primary key to null doesn’t make much sense to me. So, not a behavior issue.

Thanks.