相关疑难解决方法(0)

在postPersist事件中插入主义

我想在实体持久化和更新上添加新的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了

完整性约束违规:1062重复条目'30 -2'用于键'PRIMARY'

在日志中有两个插入:

INSERT INTO interview_scientificdirection(interview_id,scientificdirection_id)VALUES(?,?)([30,2])INSERT INTO interview_scientificdirection(interview_id,scientificdirection_id)VALUES(?,?)([30,2])

scientificdirection是多对多的关系表,对于我们想要坚持的实体.在前端应用程序一切正常,但在索纳塔管理员我遇到了这个问题:(

symfony doctrine-orm symfony-sonata

16
推荐指数
2
解决办法
4万
查看次数

标签 统计

doctrine-orm ×1

symfony ×1

symfony-sonata ×1