When creating an item in a relation manager, the beforeSave event is called twice.
I seem to be getting the following error, which might be related as I can see the makeManageWidget function being hit twice if I debug.
"get_class() expects parameter 1 to be object, null given" on line 836 of modules/backend/Behaviors/RelationController.php
Happens with 450/451 but is fine with 447, I haven't tried 448/449 using PHP 7.3.
@AugmentBLU that seems like the actual error you're getting is that the model wasn't found: https://github.com/octobercms/october/blob/develop/modules/backend/behaviors/RelationController.php#L833. That code probably needs to be fixed to correct the error message thrown
I having same issue, anyway to prevent this?
For now if you want to call something in afterSave you need to check timestamps if they are totally same just ignore it, something like this:
$skipCreating = $myOtherModel ? ($myOtherModel->created_at == now()->toDateTimeString()) : false;
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.
@CptMeatball @AugmentBLU @Samuell1 So I can explain the "why" this is happening, but I cannot explain the "why" it was set up this way.
The Relation Controller saves the Comment on this line here based on the POST data:
https://github.com/octobercms/october/blob/c1fe12f732278df8a706fccf62baae8cd918ca30/modules/backend/behaviors/RelationController.php#L1099
Once this is done, it runs $this->relationObject->add in order to, I believe, associate the new Comment model with the Post model:
https://github.com/octobercms/october/blob/c1fe12f732278df8a706fccf62baae8cd918ca30/modules/backend/behaviors/RelationController.php#L1102
However, this also calls the save method here:
https://github.com/octobercms/library/blob/4bda08c826fc3ac1efba32fa8799d25ced14191f/src/Database/Relations/HasOneOrMany.php#L60
Which would mean the beforeSave event is fired twice.
I tested it by removing the $this->relationObject->add, and the relation seemed to save correctly and the beforeSave event was only fired once. It's likely because the necessary relation information is already saved here:
So I wonder if that particular call is even needed. Hopefully someone with a bit more knowledge with the Relation manager might be able to chime in.
This pull request should hopefully resolve it: https://github.com/octobercms/october/pull/4723
Most helpful comment
@CptMeatball @AugmentBLU @Samuell1 So I can explain the "why" this is happening, but I cannot explain the "why" it was set up this way.
The Relation Controller saves the Comment on this line here based on the POST data:
https://github.com/octobercms/october/blob/c1fe12f732278df8a706fccf62baae8cd918ca30/modules/backend/behaviors/RelationController.php#L1099
Once this is done, it runs
$this->relationObject->addin order to, I believe, associate the new Comment model with the Post model:https://github.com/octobercms/october/blob/c1fe12f732278df8a706fccf62baae8cd918ca30/modules/backend/behaviors/RelationController.php#L1102
However, this also calls the
savemethod here:https://github.com/octobercms/library/blob/4bda08c826fc3ac1efba32fa8799d25ced14191f/src/Database/Relations/HasOneOrMany.php#L60
Which would mean the
beforeSaveevent is fired twice.I tested it by removing the
$this->relationObject->add, and the relation seemed to save correctly and thebeforeSaveevent was only fired once. It's likely because the necessary relation information is already saved here:https://github.com/octobercms/october/blob/master/modules/backend/behaviors/RelationController.php#L1091-L1094
So I wonder if that particular call is even needed. Hopefully someone with a bit more knowledge with the Relation manager might be able to chime in.