Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Model::duplicate #801

Merged
merged 9 commits into from
Dec 26, 2020
Merged

[fix] Model::duplicate #801

merged 9 commits into from
Dec 26, 2020

Conversation

georgehristov
Copy link
Collaborator

No description provided.

src/Model.php Outdated
@@ -1178,12 +1178,14 @@ public function reload()
*/
public function duplicate($newId = null)
{
$this->setId(null);
$this->entityId = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity approach must satisty several axioms. One of them is to never allow to assing different ID to the same instance.

So I think we should remove duplicate completely or clone before at the beginning of this method and return the cloned model then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of method is not good option as functionality is useful.
Cloning model and returning it is a good option but $entityId is private. how to clear it best?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came up with following code but it does not work as even if model unloaded $entityId value remains saved. I think this is not the correct behavior

    public function duplicate($newId = null): self
    {
        $duplicate = clone $this;

        $duplicate->unload();

        $duplicate->setMulti($this->data);

        if ($duplicate->id_field) {
            $duplicate->setId($newId);
        }

        return $duplicate;
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply set entityId to null after clone, + then set whateer needed (unload, reset dirty, do your research)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make it work but code is not consistent.
The problem I see is that clearing entityId should be done with unload. What is unload doing otherwise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone should not normally do these things

as a design rule, otherwise there is not a real issues

but to satisfy standards as well, we should do that in special method

now in duplciate() :)

Copy link
Collaborator Author

@georgehristov georgehristov Dec 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still of the opinion that if we support unload then entityId should be cleared. All other values are cleared.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got me for 1, 2 seconds. The answer is clear no.

Entity approach must satisty several axioms. One of them is to never allow to assing different ID to the same instance.

So I think we should remove duplicate completely or clone before at the beginning of this method and return the cloned model then.

because if we allow to unset entityId, it can be easily changed, and this axiom will be violated. This any code that clears entityId is dangerous (except you know what you are dooing or do that on a fresh clone)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should remove unload method as it is not working properly at the moment or just use it to return a fresh object without entity loaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unload unloads data, but keep entity bound to a specific ID

I think there are usecases for it.

Copy link
Member

@DarkSide666 DarkSide666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works like this (also clears entityId) then it's fine with me.

BUT I think that probably we have to finish refactoring this Model / Entity situation and then quite many internal things will change anyway. Currently this Model/Entity approach looks wrong and hard to use and understand.

Copy link
Member

@mvorisek mvorisek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main issues (cloning, no $newId) resolved

@georgehristov georgehristov merged commit b495447 into develop Dec 26, 2020
@georgehristov georgehristov deleted the fix/model-duplicate branch December 26, 2020 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants