简体   繁体   English

当实体更新时,cascade = {“ persist”}不起作用(但在创建时起作用)

[英]cascade={“persist”} doesn't work when entity is updated (but it does at the creation)

I have two Entities with a relation ManyToOne between them. 我有两个实体,它们之间有一个ManyToOne关系。

My first entity look like this: 我的第一个实体看起来像这样:

<?php
    [...] //namespace, use and stuff
/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Project\MyBundle\Entity\Entity1Repository")
 * @ORM\HasLifecycleCallbacks()
 */
class Entity1
{
    [...] //id and stuff
//
    /**
     * @ORM\OneToMany(targetEntity="Project\MyBundle\Entity\Entity2", mappedBy="entity1", cascade={"persist"})
     */
    private $entities2;
//
    public function __construct() {
        $this->entities2 = new ArrayColleciont();
        [...]
    }
    [...] // Getter Setter and stuff
//
    /**
     *@ORM\PreUpdate()
     *@ORM\PrePersist()
     */
    public function newEntity2() {
        $this->entities2->add(new Entity2($this));
    }

And my second look like that: 我的第二个样子是:

<?php
    [...]//namespace, use and stuff
/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Project\MyBundle\Entity\Entity2Repository")
 */
class Entity2
{
    [...]// id and stuff
//
    /**
     * @ORM\ManyToOne(targetEntity="Project\MyBundle\Entity\Entity1", inversedBy="Entities2")
     */
    private $entity1;
//
    public function __construct(Entity1 $entity1) {
        $this->entity1 = $entity1;
        [...]
    }

The point is. 重点是。 When I create a new Entity1 and I persist it, a Entity2 is created too and saved in database. 当我创建一个新的Entity1并将其持久化时,也会创建一个Entity2并将其保存在数据库中。 BUT, when I simply update my Entity1 the method "newEntity2" is still called but nothing is saved in database. 但是,当我仅更新Entity1时,仍将调用方法“ newEntity2”,但数据库中未保存任何内容。

My guess is, it come from the cascade="persist" option. 我的猜测是,它来自cascade =“ persist”选项。

From the documentation on lifecycle events: 从有关生命周期事件的文档中

prePersist - The prePersist event occurs for a given entity before the respective EntityManager persist operation for that entity is executed. prePersist-给定实体的prePersist事件在执行该实体的相应EntityManager持久操作之前发生。 It should be noted that this event is only triggered on initial persist of an entity (ie it does not trigger on future updates). 应该注意的是, 该事件仅在实体的初始持久存在时才触发 (即,它不会在以后的更新时触发)。

So persist will only be useful on the first load. 因此,持久性仅在首次加载时才有用。

From the documentation on preUpdate 从preUpdate的文档

Changes to associations of the updated entity are never allowed in this event 在这种情况下,绝不允许更改已更新实体的关联

Changes to fields of the passed entities are not recognized by the flush operation 刷新操作无法识别对传递的实体的字段所做的更改

So preUpdate is not what you want. 因此,preUpdate不是您想要的。

I would look into implementing an event listener , probably onFlush , you can do whatever you please in this event. 我将研究实现一个事件侦听器 ,可能是onFlush ,您可以在此事件中做任何您想做的事情。

preUpdate is too late to change anything. preUpdate为时已晚,无法更改任何内容。 The changes already calculated and doctrine know what sqls to run to save your changes. 已经计算出的更改和理论知道要运行哪些sql以保存更改。 (it is effectively happens before running the sql update) (实际上是在运行sql更新之前发生的)

onFlush is a better option if you want to track changes (will be fired on creation of your entities also). 如果要跟踪更改(最好在创建实体时也会触发),onFlush是更好的选择。

Just don't forget, that at this stage, if you change a property or create / delete an entity, you need to ask doctrine manually to re-check the changes you made. 只是不要忘记,在此阶段,如果您更改属性或创建/删除实体,则需要手动询问原则以重新检查所做的更改。 (read the lines under the code sample in doctrine docs: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#onflush ) (请阅读教义文档中代码示例下的代码行: http : //doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#onflush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM