The old collection not deleted after a sequence of two flushes #5771

Closed
opened 2026-01-22 15:17:23 +01:00 by admin · 1 comment
Owner

Originally created by @vtsykun on GitHub (Nov 19, 2017).

Originally assigned to: @Majkl578 on GitHub.

When you in full rewrite a collection for an entity, sometimes an old collection is not removed from the database.

Example

class Contact
{
    /**
     * @Id
     * @Column(type="integer")
     */
    public $id;

    /**
     * @Column(type="string")
     */
    public $name;

    /**
     * @ManyToMany(targetEntity="Source")
     */
    public $sources;
}
class Source
{
    /**
     * @Id
     * @Column(type="string")
     */
    public $id;
}

Origin entity

id name sources
1 name 1 cck
$contact->name = 'name 2';
$em->flush();

$sources = $contact->sources->toArray();
$contact->sources = new ArrayCollection($sources);
$em->flush();

After the first flush, the changes for the collection fields are no longer tracked and an error occurred during the second flush.

SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "gh6829contact_gh6829source_pkey"
DETAIL:  Key (gh6829contact_id, gh6829source_id)=(1, cck) already exists.

If you remove the first flush then it works fine.

$contact->name = 'name 2';
$sources = $contact->sources->toArray();
$contact->sources = new ArrayCollection($sources);
$em->flush();
Originally created by @vtsykun on GitHub (Nov 19, 2017). Originally assigned to: @Majkl578 on GitHub. When you in full rewrite a collection for an entity, sometimes an old collection is not removed from the database. Example ``` class Contact { /** * @Id * @Column(type="integer") */ public $id; /** * @Column(type="string") */ public $name; /** * @ManyToMany(targetEntity="Source") */ public $sources; } ``` ``` class Source { /** * @Id * @Column(type="string") */ public $id; } ``` Origin entity | id | name | sources | |:--:|:--:|:--:| |1|name 1| cck | ```php $contact->name = 'name 2'; $em->flush(); $sources = $contact->sources->toArray(); $contact->sources = new ArrayCollection($sources); $em->flush(); ``` After the first flush, the changes for the collection fields are no longer tracked and an [error](https://travis-ci.org/doctrine/doctrine2/jobs/303710005#L612) occurred during the second flush. ``` SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "gh6829contact_gh6829source_pkey" DETAIL: Key (gh6829contact_id, gh6829source_id)=(1, cck) already exists. ``` If you remove the first flush then it works fine. ```php $contact->name = 'name 2'; $sources = $contact->sources->toArray(); $contact->sources = new ArrayCollection($sources); $em->flush(); ```
admin added the BugInvalid labels 2026-01-22 15:17:23 +01:00
admin closed this issue 2026-01-22 15:17:24 +01:00
Author
Owner

@Majkl578 commented on GitHub (Dec 16, 2017):

This is by design as it is now, changing collection itself is not supported and must be kept as PersistentCollection assigned by UnitOfWork.

@Majkl578 commented on GitHub (Dec 16, 2017): This is by design as it is now, changing collection itself is not supported and must be kept as PersistentCollection assigned by UnitOfWork.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5771