问题描述
我在使用PreUpdate HasLifecycleCallbacks时遇到一些问题。
I have a bit of problems with PreUpdate HasLifecycleCallbacks.
我有一个实体,比方说 A与实体 B具有OneToOne关系。
所以我有:
I have an entity, let say "A" with have a OneToOne relation with the entity "B".So I have:
/**
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
*/
class A
{
/**
* @ORM\OneToOne(targetEntity="B", inversedBy="attrA", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="fieldB", referencedColumnName="id")
*/
private $attrB;
public function __construct()
{
$this->attrB = new B();
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updateSomthing(){
//$gestor = fopen("/pruebitas.txt", "r");
$this->attrB->setDate($this->getDate());
}
}
B类为:
class B
{
/**
* @ORM\OneToOne(targetEntity="A", mappedBy="attrB")
*/
private $attrA;
}
当我创建新实体A时,一切正常,问题是我更新了实体A,PreUpdate函数启动了(因为它在注释行中创建了文件),但是即使应更新B中的字段,实体B也不持久存在于数据库中。
When I create a new entity A, everything works fine, the problem is when I update the entity A, the PreUpdate function is fire, (because it creates the file in the commented line), but the entity B does not persist in the database, even if the field in B should be updated.
任何在PreUpdate上级联持久化的想法吗?
Any idea to cascade the persist on the PreUpdate??
谢谢!
推荐答案
使用 preFlush
代替
来自Doctrine文档 preUpdate
事件:
这是有道理的,因此在工作单元生成所有变更集之前,您需要对关联实体进行变更。这就是 preFlush
事件的目的。
That makes sense, so you need to do your changes to associated entities before all the changesets are genereated by the Unit of Work. That's what the preFlush
event is for.
只需替换您的 @带有
批注,它应该可以使用。 @ ORM\PreFlush
的ORM\PreUpdate
Simply replace your @ORM\PreUpdate
annotation with @ORM\PreFlush
and it should work.
preFlush
事件自Doctrine 2.2起可用。
The preFlush
event is available since Doctrine 2.2.
这篇关于PreUpdate级联实体保留symfony 2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!