首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >原则2梯级持续节省过多

原则2梯级持续节省过多
EN

Stack Overflow用户
提问于 2015-10-30 18:50:30
回答 1查看 111关注 0票数 0

这是我的prePersist on EventListener

代码语言:javascript
复制
public function prePersist(LifecycleEventArgs $args)
    {
        //the first entity will have the PMP, so we catch it and continue to skip this if after this
        if ($this->pmp == null) {
            $this->pmp = $args->getEntity()->getPmp();
        }

        $taxonomicClass = $args->getEntity();

        if($taxonomicClass instanceof TaxonomicClass){

            if(is_null($taxonomicClass->getId())){
                //here it says that i have created a new entity, need to persist it via cascade={"persist"}
                $taxonomicClass->setPmp($this->pmp);
            }
        }
    }

没关系,我在上面添加了注释:

代码语言:javascript
复制
 /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Pmp", cascade={"persist"})
     * @ORM\JoinColumn(name="pmp_id", referencedColumnName="id", nullable=false)
     **/
    private $pmp;

它保存了我的层次结构中的所有东西,甚至是一个新的PMP,一个已经存在于数据库中的对象!

我想要的是,从我的层次结构中保存的所有东西都需要与我通过的PMP相关,但是当我设置$taxonomicClass->setPmp($this->pmp);原则时,我认为我创建了PMP的一个新实例,因为im不是,我只是想让这个家伙与PMP有一个关联。

我试过将merge放在级联选项上,但它只适用于persist,如何使原则不创建新实例,而使用我传递的实例呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-03 11:49:17

注意到我的问题,我从记忆中分配了一个属性,我应该把他从数据库检索到理论理解。

代码语言:javascript
复制
public function prePersist(LifecycleEventArgs $args)
{
    if ($this->pmp == null) {
            $this->pmp = $args->getEntity()->getPmp();
    }

    $taxonomicClass = $args->getEntity();

    if($taxonomicClass instanceof TaxonomicClass){

        if(is_null($taxonomicClass->getId())){
            //this solved the problem
            $pmp = $args->getEntityManager()->getRepository("AppBundle:Pmp")->find($this->pmp->getId());
            $taxonomicClass->setPmp($pmp);
        }
    }
}

现在我要记住,当创建一个新实体时,但是它不需要保存,您必须从db检索它,cascade={"persist"}甚至不是必需的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33442982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档