[英]cascade={“persist”} doesn't work when entity is updated (but it does at the creation)
我有兩個實體,它們之間有一個ManyToOne關系。
我的第一個實體看起來像這樣:
<?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));
}
我的第二個樣子是:
<?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;
[...]
}
重點是。 當我創建一個新的Entity1並將其持久化時,也會創建一個Entity2並將其保存在數據庫中。 但是,當我僅更新Entity1時,仍將調用方法“ newEntity2”,但數據庫中未保存任何內容。
我的猜測是,它來自cascade =“ persist”選項。
preUpdate為時已晚,無法更改任何內容。 已經計算出的更改和理論知道要運行哪些sql以保存更改。 (實際上是在運行sql更新之前發生的)
如果要跟蹤更改(最好在創建實體時也會觸發),onFlush是更好的選擇。
只是不要忘記,在此階段,如果您更改屬性或創建/刪除實體,則需要手動詢問原則以重新檢查所做的更改。 (請閱讀教義文檔中代碼示例下的代碼行: http : //doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#onflush )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.