Doctrine 2.1 - Relation Lost After ManyToMany Cascade Merge - Symfony2

十年热恋 提交于 2019-12-10 14:47:47

问题


After merging an entity that has related entities with relations set to cascade both persist and merge operations the relations 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 joins:

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 output:

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'm being dumb?

BTW there are more relationships than this. I've simplified things here to make the explanation clearer. My other relations aren't affected negatively by the merge. Also I'm doing the merge in a console Command not a in a controller. I can post more code if it'll help :)

Also I'm using Doctrine 2.1.7 and Symfony 2.0.15

Many thanks, Matthew


回答1:


I've faced the same issue, and fixed it by refreshing the entity after merge:

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



回答2:


i found the same problem and after think some minutes i tried a simple think, just change code order and treat the merged as new instance of the entity, i figure out that the merge action can be removing the relations, so:

$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();

Its works for me.

I hope it helps.




回答3:


Is this issue not related to your merge: http://doctrine-orm.readthedocs.org/en/2.0.x/reference/limitations-and-known-issues.html#cascade-merge-with-bi-directional-associations?



来源:https://stackoverflow.com/questions/11198434/doctrine-2-1-relation-lost-after-manytomany-cascade-merge-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!