duanlai1855 2015-10-30 18:50
浏览 87
已采纳

学说2级联持续存在太多

so this is my prePersist on EventListener

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);
            }
        }
    }

that's fine, i had added the annotation on it:

 /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Pmp", cascade={"persist"})
     * @ORM\JoinColumn(name="pmp_id", referencedColumnName="id", nullable=false)
     **/
    private $pmp;

and it saves everything from my hierarchy, even a new PMP, an object that already exist in the database!

what i want is that everything that im saving from my hierarchy needs to be related to the PMP that i passed, but when i set $taxonomicClass->setPmp($this->pmp); doctrine thinks that i created a new instance of PMP, since im not, i just want to this guy have an associaton with the PMP.

i tried put merge on the cascade option, but it only works with persist, how to make doctrine dont create a new instance, and instead use the one that i passed?

  • 写回答

1条回答 默认 最新

  • duan19750503 2015-11-03 11:49
    关注

    noticed my problem, i was assigning an attribute from memory, i should retrive him from database to doctrine understand.

    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);
            }
        }
    }
    

    i will keep in mind now that when a new entity is created, but it doesn't need to be saved, you must retrieve it from db, cascade={"persist"} wasn't even necessary

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢