从某种意义上说,我想将我的旧问题Flushing in postPersist possible or not?转移到prePersist
事件中。
假设以下示例
class Entity {
/** @PrePersist */
public function doStuffOnPrePersist(LifecycleEventArgs $args)
{
$this->value = 'changed from prePersist callback!';
$other = new OtherEntity();
$args->getEntityManager()->persist($other);
}
}
叫
$entity = new Entity();
$entity->value = "unchanged";
$em->persist($entity);
$em->flush();
我有两个基本问题:
value
是“不变”还是“从...更改”?OtherEntity
写入数据库吗?假设两个问题的回答都是肯定的,让我扩展我的问题:
Entity
和OtherEntity
都有一个自动生成的ID字段,而OtherEntity
拥有对Entity
的引用(ManyToOne):
class Entity {
/*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/** @PrePersist */
public function doStuffOnPrePersist(LifecycleEventArgs $args)
{
$this->value = 'changed from prePersist callback!';
$other = new OtherEntity();
$other->father = $this; // <-------------
$args->getEntityManager()->persist($other);
}
}