duandong2562 2018-08-18 09:45
浏览 50
已采纳

ManyToMany关系不会持久化

I have Post And PostTag entities. Only custom users are allowed create PostTags. While creating a post user may select post tags.

I have created many to many relationship between Post And PostTag entities using the make:entity command.

The problem is that after creating the post and attaching to it the selected tags the relation table is empty and nothing is returned by post.getPostTags() method.

PostController - create method:

...
    $em = $this->getDoctrine()->getManager();

    $post = $form->getData();
    $post->setAuthor($this->getUser());
    $post->setCreatedAt(new DateTime);
    foreach ($form->get('post_tags')->getData() as $postTag) {
         $post->addPostTag($postTag);
    }
    $em->persist($post);
    $em->flush();
...

Post entity:

     /**
     * @ORM\ManyToMany(targetEntity="App\Entity\PostTag", mappedBy="post", cascade={"persist"})
     */
    private $postTags;



    public function __construct()
    {
        $this->postTags = new ArrayCollection();
    }

    /**
     * @return Collection|PostTag[]
     */
    public function getPostTags(): Collection
    {
        return $this->postTags;
    }

    public function addPostTag(PostTag $postTag): self
    {
        if (!$this->postTags->contains($postTag)) {
            $this->postTags[] = $postTag;
            $postTag->addPost($this);
        }

        return $this;
    }

    public function removePostTag(PostTag $postTag): self
    {
        if ($this->postTags->contains($postTag)) {
            $this->postTags->removeElement($postTag);
            $postTag->removePost($this);
        }

        return $this;
    }

PostTag entity:

     /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Post", inversedBy="postTags", cascade={"persist"})
     */
    private $post;

    public function __construct()
    {
        $this->post = new ArrayCollection();
    }
    /**
     * @return Collection|Post[]
     */
    public function getPost(): Collection
    {
        return $this->post;
    }

    public function addPost(Post $post): self
    {
        if (!$this->post->contains($post)) {
            $this->post[] = $post;
        }

        return $this;
    }

    public function removePost(Post $post): self
    {
        if ($this->post->contains($post)) {
            $this->post->removeElement($post);
        }

        return $this;
    }
  • 写回答

1条回答 默认 最新

  • douran7929 2018-08-19 15:08
    关注

    You have set the PostTag entity as the owning side (by virtue of setting inversedBy parameter on that entity), so you have to persist the PostTag entity, not the Post entity, to make things stick. You can make a simple modification to your Post entity to make this work:

    public function addPostTag(PostTag $postTag): self
    {
        if (!$this->postTags->contains($postTag)) {
            $this->postTags[] = $postTag;
            $postTag->addPost($this);
    
            // set the owning side of the relationship
            $postTag->addPost($this);
        }
    
        return $this;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂