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

There is the fail "UNIQUE constraint failed" with a LAZY collection #6613

Closed
Sysaninster opened this issue Aug 11, 2017 · 5 comments · Fixed by #6616
Closed

There is the fail "UNIQUE constraint failed" with a LAZY collection #6613

Sysaninster opened this issue Aug 11, 2017 · 5 comments · Fixed by #6616
Assignees
Milestone

Comments

@Sysaninster
Copy link
Contributor

I have the User entity with the LAZY collection field. If I do this code:

        $user = new User();
        $user->id = 1;
        $this->_em->persist($user);
        $this->_em->flush();

        $this->_em->clear();

        /** @var User $user */
        $user = $this->_em->find(User::class, 1);
        $phone1 = new Phone();
        $phone1->id = 1;
        $user->phones->add($phone1);
        $this->_em->persist($phone1);
        $this->_em->flush();

        $phone2 = new Phone();
        $phone2->id = 2;
        $user->phones->add($phone2);
        $this->_em->persist($phone2);
        $user->phones->toArray();
        $this->_em->flush();

I will get the error:

Doctrine\DBAL\Exception\UniqueConstraintViolationException: An exception occurred while executing 'INSERT INTO user_phone (user_id, phone_id) VALUES (?, ?)' with params [1, 1]:

SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: user_phone.user_id, user_phone.phone_id

If I remove the $user->phones->toArray(); line then it will work without a problem.

@Ocramius
Copy link
Member

@Ocramius
Copy link
Member

Closing here, thanks for sending a failing test case!

@Ocramius
Copy link
Member

Ref: #6614

Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…ng`), segregating phone creation away
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…are restored in the dirty state of the collection
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
@Ocramius
Copy link
Member

Reopening, since this will be closed once #6616 is merged.

@Ocramius Ocramius reopened this Aug 11, 2017
@Ocramius Ocramius added this to the 2.5.7 milestone Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…toreNewObjectsInDirtyCollection` implementation
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…ng`), segregating phone creation away
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
… are restored in the dirty state of the collection
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…erifies basic dirty collection initialization semantics
Ocramius added a commit that referenced this issue Aug 11, 2017
…that are also coming from initialization data de-duplicates new and persisted items
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
… if all new items are contained in the initialized ones
Ocramius added a commit that referenced this issue Aug 11, 2017
…toreNewObjectsInDirtyCollection` implementation
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
…y-object-persistence-2.5' into 2.5

Backport #6613
Backport #6614
Backport #6616
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
Ocramius added a commit that referenced this issue Aug 11, 2017
@Ocramius
Copy link
Member

@Sysaninster thanks for the bug report - this is fixed and will go into 2.5.7 ASAP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants