DDC-1593: Duplicate associations with flush on PostPersist #2000

Closed
opened 2026-01-22 13:37:11 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 10, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user lutangar:

Hi,

I am playing around this issue for days, maybe it is more a Doctrine2 issue or a simple logic issue, but i cant tell :

Yaml mapping:

SG\TestMediaBundle\Entity\Article:
    type: entity
    table: sg*test*article
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        title:
            type: string
            length: '255'
            nullable: false
    manyToMany:
        images:
            targetEntity: SG\TestMediaBundle\Entity\Image
            cascade: [ persist, remove ]

Controller function :

   public function indexAction()
    {
        $content = new \SG\TestMediaBundle\Entity\Article();
        $content->getImages()->add(new \SG\TestMediaBundle\Entity\Image());
        $content->getImages()->add(new \SG\TestMediaBundle\Entity\Image());

        $form = $this->createFormBuilder($content)
        ->add('title', null, array('label'=>'title'))
        ->add('images', 'collection', array('type' => new \SG\TestMediaBundle\Form\Type\ImageFormType(), 'label'=>'images'))
        ->getForm();

        if ($this->getRequest()->getMethod() == 'POST') {
            $form->bindRequest($this->getRequest());
            if ($form->isValid()) {
                $em = $this->container->get('doctrine')->getEntityManager();
                $em->persist($content);
                $em->flush();
            }
        }
        return $this->render('SGTestMediaBundle:Default:index.html.twig', array('form'=>$form->createView()));
    }

Here is the listener :

class ImageListener
{
    public function getSubscribedEvents()
    {
        return array(
            Events::postPersist,
        );
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $args->getEntityManager()->flush();
    }
}

And here with I got on form submit :

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY'

  1. at PDOStatement ->execute (array('1', '1'))
  2. at Connection ->executeUpdate ('INSERT INTO article_image (article_id, image_id) VALUES (?, ?)', array('1', '1'))
  3. at AbstractCollectionPersister ->insertRows (object(PersistentCollection))
  4. at AbstractCollectionPersister ->update (object(PersistentCollection))
  5. at UnitOfWork ->commit ()
  6. at EntityManager ->flush ()
  7. at ImageListener ->postPersist (object(LifecycleEventArgs))
  8. at EventManager ->dispatchEvent ('postPersist', object(LifecycleEventArgs))
  9. at UnitOfWork ->executeInserts (object(ClassMetadata))
  10. at UnitOfWork ->commit ()
  11. at EntityManager ->flush ()
  12. at ImageListener ->postPersist (object(LifecycleEventArgs))
  13. at EventManager ->dispatchEvent ('postPersist', object(LifecycleEventArgs))
  14. at UnitOfWork ->executeInserts (object(ClassMetadata))
  15. at UnitOfWork ->commit ()
  16. at EntityManager ->flush ()
  17. at DefaultController ->indexAction ()

I writed down a test bundle i could post it on github if needed.

Cheers.

Originally created by @doctrinebot on GitHub (Jan 10, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user lutangar: Hi, I am playing around this issue for days, maybe it is more a Doctrine2 issue or a simple logic issue, but i cant tell : Yaml mapping: ``` SG\TestMediaBundle\Entity\Article: type: entity table: sg*test*article id: id: type: integer generator: strategy: AUTO fields: title: type: string length: '255' nullable: false manyToMany: images: targetEntity: SG\TestMediaBundle\Entity\Image cascade: [ persist, remove ] ``` Controller function : ``` public function indexAction() { $content = new \SG\TestMediaBundle\Entity\Article(); $content->getImages()->add(new \SG\TestMediaBundle\Entity\Image()); $content->getImages()->add(new \SG\TestMediaBundle\Entity\Image()); $form = $this->createFormBuilder($content) ->add('title', null, array('label'=>'title')) ->add('images', 'collection', array('type' => new \SG\TestMediaBundle\Form\Type\ImageFormType(), 'label'=>'images')) ->getForm(); if ($this->getRequest()->getMethod() == 'POST') { $form->bindRequest($this->getRequest()); if ($form->isValid()) { $em = $this->container->get('doctrine')->getEntityManager(); $em->persist($content); $em->flush(); } } return $this->render('SGTestMediaBundle:Default:index.html.twig', array('form'=>$form->createView())); } ``` Here is the listener : ``` class ImageListener { public function getSubscribedEvents() { return array( Events::postPersist, ); } public function postPersist(LifecycleEventArgs $args) { $args->getEntityManager()->flush(); } } ``` And here with I got on form submit : > SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' 1. at PDOStatement ->execute (array('1', '1')) 2. at Connection ->executeUpdate ('INSERT INTO article_image (article_id, image_id) VALUES (?, ?)', array('1', '1')) 3. at AbstractCollectionPersister ->insertRows (object(PersistentCollection)) 4. at AbstractCollectionPersister ->update (object(PersistentCollection)) 5. at UnitOfWork ->commit () 6. at EntityManager ->flush () 7. at ImageListener ->postPersist (object(LifecycleEventArgs)) 8. at EventManager ->dispatchEvent ('postPersist', object(LifecycleEventArgs)) 9. at UnitOfWork ->executeInserts (object(ClassMetadata)) 10. at UnitOfWork ->commit () 11. at EntityManager ->flush () 12. at ImageListener ->postPersist (object(LifecycleEventArgs)) 13. at EventManager ->dispatchEvent ('postPersist', object(LifecycleEventArgs)) 14. at UnitOfWork ->executeInserts (object(ClassMetadata)) 15. at UnitOfWork ->commit () 16. at EntityManager ->flush () 17. at DefaultController ->indexAction () I writed down a test bundle i could post it on github if needed. Cheers.
admin added the Bug label 2026-01-22 13:37:11 +01:00
admin closed this issue 2026-01-22 13:37:11 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 10, 2012):

Comment created by @beberlei:

Flush inside a listener is not allowed.

This should be catched by Doctrine, but is currently not.

@doctrinebot commented on GitHub (Jan 10, 2012): Comment created by @beberlei: Flush inside a listener is not allowed. This should be catched by Doctrine, but is currently not.
Author
Owner

@doctrinebot commented on GitHub (Jan 10, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 10, 2012): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2000