教义2.1 - 关系失去了许多级联合并 - Symfony2 [英] Doctrine 2.1 - Relation Lost After ManyToMany Cascade Merge - Symfony2

查看:117
本文介绍了教义2.1 - 关系失去了许多级联合并 - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是实体:

  class Event implements NormalizableInterface 
{
/ **
* @ ORM\ManyToMany(targetEntity =参与者,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 $ participant;
}


class参与者实现NormalizableInterface
{
/ **
* @ ORM\ManyToMany(targetEntity =Event mappedBy =参与者,cascade = {persist,merge})
* /
protected $ events;
}

这是我的连接:



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

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

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

这是我的合并代码:

  echo之前合并参与者计数:.count($ event-> getParticipants())PHP_EOL; 

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

echo合并参与者数:.count($ event-> getParticipants())。PHP_EOL;

...这里是输出:



合并参与者数:2
合并参与者数:0

..数据库如下所示:

  table |行
-------------------------------
事件| 1
-------------------------------
参与者| 2
-------------------------------
event_participant | 0
-------------------------------

任何人都可以看到我在愚蠢的地方吗?



BTW有更多的关系。我在这里简化了一些事情,使解释更清晰。我的其他关系并没有受到合并的负面影响。另外我在控制台中的控制台命令不是一个合并。我可以发布更多的代码,如果它会帮助:)



另外我使用的是Doctrine 2.1.7和Symfony 2.0.15



非常感谢Matthew

解决方案

我遇到同样的问题,并通过刷新实体来修复合并后:

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


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

解决方案

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

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

这篇关于教义2.1 - 关系失去了许多级联合并 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆