Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:45:54+00:00 2026-06-05T09:45:54+00:00

I have this entity: <?php namespace Comakai\MyBundle\Entity; use Doctrine\ORM\Mapping as ORM, Symfony\Component\Validator\Constraints as Assert;

  • 0

I have this entity:

<?php

namespace Comakai\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM,
    Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Stuff {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $content;

    /**
     * @ORM\ManyToMany(targetEntity="Apple", cascade={"persist"})
     */
    private $apples;

    /**
     * @ORM\ManyToMany(targetEntity="Pig")
     */
    private $pigs;

    public function __construct() {
        $this->apples = new \Doctrine\Common\Collections\ArrayCollection();
        $this->pigs = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function setApples($apples) {

        $this->getApples()->clear();

        foreach ($apples as $apple) {

            $this->addApple($apple);
        }
    }

    public function setPigs($pigs) {

        $this->getPigs()->clear();

        foreach ($pigs as $pig) {

            $this->addPig($pig);
        }
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set content
     *
     * @param text $content
     */
    public function setContent($content) {
        $this->content = $content;
    }

    /**
     * Get content
     *
     * @return text 
     */
    public function getContent() {
        return $this->content;
    }

    /**
     * Add apples
     *
     * @param Comakai\MyBundle\Entity\Apple $apples
     */
    public function addApple(\Comakai\MyBundle\Entity\Apple $apples) {
        $this->apples[] = $apples;
    }

    /**
     * Get apples
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getApples() {
        return $this->apples;
    }


    /**
    * Add pigs
    *
    * @param Comakai\MyBundle\Entity\Pig $pigs
    */
    public function addPig(\Comakai\MyBundle\Entity\Pig $pigs) {
        $this->pigs[] = $pigs;
    }

    /**
    * Get pigs
    *
    * @return Doctrine\Common\Collections\Collection 
    */
    public function getPigs() {
        return $this->pigs;
    }
}

and this listener:

<?php

namespace Comakai\MyBundle\Listener;

use Comakai\MyBundle\Util\SluggerParser
    Doctrine\ORM\Event\OnFlushEventArgs,
    Comakai\MyBundle\Entity\Stuff,
    Comakai\MyBundle\Entity\Apple,
    Comakai\MyBundle\Entity\Pig;

class Listener {

    /**
     * @param \Doctrine\ORM\Event\OnFlushEventArgs $ea
     */
    public function onFlush(OnFlushEventArgs $ea) {

        $em = $ea->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() AS $entity) {

            $this->save($entity, $em, $uow);

        }

        foreach ($uow->getScheduledEntityUpdates() AS $entity) {

            $this->save($entity, $em, $uow);

        }
    }

    public function save($entity, $em, $uow) {

        if ($entity instanceof Stuff) {

            $pigRepository = $em->getRepository('Comakai\MyBundle\Entity\Pig');
            $content = $entity->getContent();

            preg_match_all('/@@ pig:(\d+) @@/i', $content, $matches);
            $entity->getPigs()->clear();

            foreach($matches[1] as $pigID) {

                $pig = $pigRepository->find($pigID);

                if(!empty($pig)) {

                    $entity->addPig($pig);

                }

            }

            $entity->setContent($content);

            $meta = $em->getClassMetadata(get_class($entity));
            $uow->recomputeSingleEntityChangeSet($meta, $entity);
            $uow->computeChangeSet($meta, $entity);

        }
    }

}

And it works fine if apple’s collection is empty, but if it has some item I get a duplication error.

How can I tell to the UnitOfWork that I only want to recalculate the pig’s collection?

UPDATE

There is a new preFlush event (https://github.com/doctrine/doctrine2/pull/169) and I think this kind of things can be done there. That PR is not in the branch I’m using but let’s try it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:45:56+00:00Added an answer on June 5, 2026 at 9:45 am

    When updating an entity during a listener’s onFlush event, all you need to call is computeChangeSet():

    // make changes to entity
    $entity->field = 'value';
    
    // or assign an existing entity to an assocation
    $entity->user = $myExistingUserEntity;
    $entity->tags->add($myExistingTagEntity);
    
    $meta = $em->getClassMetadata(get_class($entity));
    $uow->computeChangeSet($meta, $entity);
    

    If you’re creating other entities too, you need to persist them and compute their changes first!

    $myNewUserEntity = new Entity\User;
    $myNewTagEntity = new Entity\Tag;
    
    $entity->user = $myNewUserEntity;
    // make sure you call add() on the owning side for *ToMany associations
    $entity->tags->add($myNewTagEntity);
    
    $em->persist($myNewUserEntity);
    $em->persist($myNewTagEntity);
    
    $metaUser = $em->getClassMetadata(get_class($myNewUserEntity));
    $uow->computeChangeSet($metaUser, $myNewUserEntity);
    
    $metaTag = $em->getClassMetadata(get_class($myNewTagEntity));
    $uow->computeChangeSet($metaTag, $myNewTagEntity);
    
    $meta = $em->getClassMetadata(get_class($entity));
    $uow->computeChangeSet($meta, $entity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my Entity: <?php namespace Trade\TradeBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as
I have this entity (Registro): <?php namespace Gitek\RegistroBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** *
I have this file: #src/Jander/JanderBundle/resources/config/doctrine/metadata/orm/ Propuestas.orm.yml Propuestas: type: entity table: propuestas fields: id: id:
I have an Entity class that looks like this: <?php namespace Entities; /** @Entity
I have an Entity called Product, this entity, through table mapping, merges 6 tables
I have entity Product and entity Subcategory : Subcategory.php namespace Project\Entities; /** * Subcategory
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
If I have this entity: @Entity class Pet { @Id long id; public enum
I'm using Hibernate 3.6.7-Final and Spring 3.0.5. I have entity like this @Entity public
I have this type of data: entity, account, month1, month2, ..., month12 abc, 2000.02,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.