我在使用 Doctrine/Symfony2 prePersist/preUpdate 监听器时遇到了问题。
多对一关系中有两个实体(一个位置可以被多个人引用)。我构建了一个表单,我在其中执行了一些 AJAX-Stuff 来帮助用户为已经存在的新人员选择一个位置。因此,我使用的是“entity_id”表单域。我还想让用户可以为数据库中不存在的新人员创建新位置。这由表单中的第二个字段处理,用户可以在其中插入新位置的名称。在持久化人的实体时,我正在检查数据库中是否存在引用的位置。如果没有,我正在生成一个新的位置实体。这是(简而言之)我在 Entity-Class Person 中的 prePersist-Lifecyclecallback:
public function prePersist() {
if($this->ort == null)
$this->ort = new Ort($this->ortsname);
}
当我创建一个新人时,这非常有效。问题是更新程序。因此,当之前有一个位置与此人相关联,并且我想通过相同的过程(使用 preUpdate-Listener)创建一个新位置时,我会收到如下通知:
Notice: Undefined index: 000000004a0010250000000058426bec in ...Doctrine/ORM/UnitOfWork.php line 983
我不知道如何解决这个问题。我认为它必须与位置对象有关,它在引用到 Person 对象之前,但我不知道如何告诉 Entity-Manager 引用到 Person 的 Location-Object 是一个新实体。我还尝试了一个 Listener-Class,例如:
if( $entity instanceof Person) {
if( $entity->getLocation() == null ) {
$entity->setLocation( new Location( $entity->getLocatioName() );
$em->persist($entity->getLocation());
$em->flush();
}
}
$em->persist ... 东西应该无关紧要,因为在映射中启用了“cascade={persist}”表示法。