我希望在实体持久化和更新时添加新的Feed项目。我编写了这个事件监听器(postUpdate相同):
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof FeedItemInterface) {
$feed = new FeedEntity();
$feed->setTitle($entity->getFeedTitle());
$feed->setEntity($entity->getFeedEntityId());
$feed->setType($entity->getFeedType());
if($entity->isFeedTranslatable()) {
$feed->getEnTranslation()->setTitle($entity->getFeedTitle('en'));
}
$em->persist($feed);
$em->flush();
}
}
但是我得到了:
完整性约束冲突:1062主键为“PRIMARY”的重复条目'30-2'
并且在日志中有两个插入操作:
INSERT INTO interview_scientificdirection (interview_id, scientificdirection_id) VALUES (?, ?) ([30,2]) INSERT INTO interview_scientificdirection (interview_id, scientificdirection_id) VALUES (?, ?) ([30,2])
scientificdirection是我们要持久化实体的多对多关系表。 在前端应用程序中一切正常,但在Sonata Admin中我遇到了这个问题 :(