Doctrine 2 - 如何获取PostPersist中最后插入的id的ID?

时间:2011-07-06 12:15:40

标签: php doctrine-orm lastinsertid

标题解释了这一切。我在实体中有一个lifecyclecallback函数。我想从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的每个实体) 感谢。

1 个答案:

答案 0 :(得分:4)

...
public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $e) {
    $newSeating = $e->getEntity();
    $id         = $newSeating->getId();
}
...