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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。