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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!