Php 原则2-如何在PostPersist中获取最后一个插入ID的ID?
标题解释了一切。我在实体中有一个lifecyclecallback函数。我想从PostPersist事件而不是实体中获取最后插入的id。举个例子,我不想这样做Php 原则2-如何在PostPersist中获取最后一个插入ID的ID?,php,doctrine-orm,lastinsertid,Php,Doctrine Orm,Lastinsertid,标题解释了一切。我在实体中有一个lifecyclecallback函数。我想从PostPersist事件而不是实体中获取最后插入的id。举个例子,我不想这样做 $newSeating = new Seat(); $newSeating->setTitle("Something"); $this->_em->persist($newSeating); $this->_em->flush(); $newSeating->getId(); 在文档中是这样写的 po
$newSeating = new Seat();
$newSeating->setTitle("Something");
$this->_em->persist($newSeating);
$this->_em->flush();
$newSeating->getId();
在文档中是这样写的
postPersist-postPersist事件
在实体之后的实体中发生
已经变得持久。会的
在数据库插入后调用
操作。生成的主键
值可在中找到
后期事件
那么如何在postPersist中获取主键值呢?
(我正在使用Mappedsuperclass,postpersist函数位于Mappedsuperclass中,因此它可用于扩展Mappedsuperclass的每个实体)
谢谢。它抛出了一个错误,postpersist需要1个参数,但给定了0。您使用的是哪一个Doctrine 2版本?能否显示注册了
postpersist
事件处理程序的相关代码?
...
public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $e) {
$newSeating = $e->getEntity();
$id = $newSeating->getId();
}
...