Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Register models only once if they are related #8224

Closed
wants to merge 2 commits into from
Closed

Register models only once if they are related #8224

wants to merge 2 commits into from

Conversation

Wusch
Copy link

@Wusch Wusch commented Feb 16, 2016

If you have a model related to itself it will be registered twice because the constructor of the model will register it. And this will create an Exception in the registry.

If you have a model related to itself it will be registered twice because the constructor of the model will register it. And this will create an Exception in the registry.
@aschempp
Copy link
Member

aschempp commented Feb 16, 2016 via email

@Wusch
Copy link
Author

Wusch commented Feb 16, 2016

We have categories for our courses and every category is followed by another one. Sometimes they just end and have no one following but some course categories repeat infinitely. That's why they are related to themself.

@aschempp
Copy link
Member

So you mean a category is related to another category. But the same category is not related to itself (same ID).

@Wusch
Copy link
Author

Wusch commented Feb 16, 2016

It can be possible that the same category is related to itself.
id => 38 followed_by => 38

@discordier
Copy link
Contributor

Which will be an endless recursion IMO.

@Wusch
Copy link
Author

Wusch commented Feb 16, 2016

No it will not because new relations are checked if they are in the registry. If not they will be created an added to the registry. But if they are already in the registry they will be merged and that's it.
The model than has a relation to itself.

The problem in the current version is that an exception is thrown because it's registered twice.

@aschempp
Copy link
Member

It can be possible that the same category is related to itself.
id => 38 followed_by => 38

as I said, this looks like an error in your application design, because it would result in an infinite recursion.

@Toflar
Copy link
Member

Toflar commented Feb 16, 2016

Which I guess is the reason to remove the register() call 😄 But I agree, it is not correct to remove that line there 😄

@Wusch
Copy link
Author

Wusch commented Feb 16, 2016

But if you remove that line there the related model will still be registered in his constructor.

@aschempp
Copy link
Member

No it will not, because there is no constructor argument so that step is skipped...

@Wusch
Copy link
Author

Wusch commented Feb 17, 2016

$objRelated = new $strClass(); on line 166 will create a new class/model?

@aschempp
Copy link
Member

Yes. But because there's no constructor argument, line 117 won't be true and the model will not be added to the registry in line 177.

@Wusch
Copy link
Author

Wusch commented Feb 17, 2016

Oh, my mistake. I updated the file and added a ne if at the second register which checks if the model is already registered. Probably that should solve the problem.

@aschempp
Copy link
Member

Now that sounds more reasonable :)
But I wonder why we would then need to build the model at all, why not check the ID in the registry first?

Also, I can't explain why this was not an issue before, because eager relations would always be loaded multiple times?

@leofeyer
Copy link
Member

@contao/developers What is the status of this PR? Shouldn't it be merged into Contao 3.5.10?

@leofeyer leofeyer added this to the 3.5.10 milestone Apr 15, 2016
@Toflar
Copy link
Member

Toflar commented Apr 15, 2016

I think no. I think this is not the right solution and I'm not sure if there is any. Can you make this up for discussion?

@leofeyer
Copy link
Member

Sure. But the current changes just make sure there is no registration attempt if the module has been registered before. That's reasonable IMHO.

@Toflar
Copy link
Member

Toflar commented Apr 15, 2016

No, it's not. It's not the responsibility of the model that creates itself to know whether it should register itself to the registry or not. That's very weird.

@Wusch
Copy link
Author

Wusch commented Apr 15, 2016

But it solves my problem and if you merge it i don't have to overwrite the file every time there is an update.

@Toflar
Copy link
Member

Toflar commented Apr 15, 2016 via email

@Wusch
Copy link
Author

Wusch commented Apr 15, 2016

If you don't want to change the model you can change the registry class on line 180 and just move on and don't throw an error.

@Toflar
Copy link
Member

Toflar commented Apr 15, 2016

That's not correct either. You cannot have two instances working for the same row, that's why I'm saying this needs to be discussed.

@leofeyer
Copy link
Member

@Wusch How did you discover the error? And how do we reproduce it?

@Wusch
Copy link
Author

Wusch commented Apr 15, 2016

We have templates for our courses where you have to select the following course. Some have no and some have another course following them. But one or two at the end of the line you can repeat infinitely. Which means that they are related to themselves.

@leofeyer leofeyer removed this from the 3.5.10 milestone Apr 19, 2016
@leofeyer leofeyer removed this from the 3.5.10 milestone Apr 19, 2016
@leofeyer leofeyer added this to the 3.5.12 milestone Apr 21, 2016
@leofeyer
Copy link
Member

As discussed in Mumble on April 21st, the following change fixes the issue:

diff --git a/system/modules/core/library/Contao/Model.php b/system/modules/core/library/Contao/Model.php
index 5ad3a48..7017407 100644
--- a/system/modules/core/library/Contao/Model.php
+++ b/system/modules/core/library/Contao/Model.php
@@ -138,6 +138,9 @@ abstract class Model

            $objRegistry = \Model\Registry::getInstance();

+           $this->setRow($arrData); // see #5439
+           $objRegistry->register($this);
+
            // Create the related models
            foreach ($arrRelated as $key=>$row)
            {
@@ -172,9 +175,6 @@ abstract class Model
                    $this->arrRelated[$key] = $objRelated;
                }
            }
-
-           $this->setRow($arrData); // see #5439
-           $objRegistry->register($this);
        }
    }

@leofeyer leofeyer modified the milestones: 3.5.12, 3.5.13 Apr 21, 2016
@leofeyer leofeyer self-assigned this Jun 9, 2016
@leofeyer
Copy link
Member

leofeyer commented Jun 9, 2016

Fixed in 12fe550.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants