Doctrine 2.1 - Communication lost after multiple cascades merge - Symfony2

After merging an object that has associated objects with relationships established to cascade both persist and merge operations, relationships are lost!

Here are the entities:

class Event implements NormalizableInterface
{
    /**
     * @ORM\ManyToMany(targetEntity="Participant", inversedBy="events", cascade={"persist", "merge"})
     * @ORM\JoinTable(name="event_participant",
     *      joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="participant_id", referencedColumnName="id", onDelete="CASCADE")}
     *      )
     */
    private $participants;
}


class Participant implements NormalizableInterface
{
    /**
     * @ORM\ManyToMany(targetEntity="Event", mappedBy="participants", cascade={"persist", "merge"})
     */
    protected $events;
}

Here are my connections:

foreach ($events as $event)
{
    if (!$event->hasParticipant($participant)) {

        $event->addParticipant($participant);
    }
    if (!$participant->hasEvent($event)) {

        $participant->addEvent($event);
    }
}

Here is my merge code:

echo "Before merge participant count: ".count($event->getParticipants()).PHP_EOL;

$event = $em->merge($event);

echo "After merge participant count: ".count($event->getParticipants()).PHP_EOL;

... and here is the result:

Before merge participant count: 2
After merge participant count: 0

.. and the database looks like this:

table               | rows
-------------------------------
event               | 1
-------------------------------
participant         | 2
-------------------------------
event_participant   | 0
-------------------------------

Can anyone see where I am dumb?

By the way, there are more such relationships. I simplified the situation to make the explanation clearer. Mergers do not affect my other relationships. I am also doing a merge in the Command console, and not in the controller. I can post more code if this helps :)

I also use Doctrine 2.1.7 and Symfony 2.0.15

Thanks a lot, Matthew

+5
source share
3

, :

$mergedEntity = $entityManager->merge($serializedEntity);
$entityManager->refresh($mergedEntity);
+3

, , , , , , , :

$user = new User();
$user->setName('name');
$user->setAge('24');

$mergedUser = $em->merge($user);

// Imagine we have some groups to add
foreach ($groups as $group) {
    $mergedUser->addGroup($group);
}

$em->flush();

.

, .

+1

All Articles