2

标题说明了一切。我在实体中有一个生命周期回调函数。我想从 PostPersist 事件而不是实体中获取最后插入的 id。作为一个我不想做的例子

$newSeating = new Seat();
$newSeating->setTitle("Something");
$this->_em->persist($newSeating);
$this->_em->flush();
$newSeating->getId();

在文档中是这样写的

postPersist - postPersist 事件在实体被持久化后发生在实体上。它将在数据库插入操作之后被调用。生成的主键值在 postPersist 事件中可用。

那么如何获取 postPersist 中的主键值呢?(我正在使用 Mappedsuperclass 并且 postpersist 函数在 Mappedsuperclass 中,因此它可用于扩展 Mappedsuperclass 的每个实体)谢谢。

4

1 回答 1

4
...
public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $e) {
    $newSeating = $e->getEntity();
    $id         = $newSeating->getId();
}
...
于 2011-07-06T12:59:06.367 回答