【问题标题】:Symfony2 - Entities not linking via ManyToManySymfony2 - 实体不通过多对多链接
【发布时间】:2014-05-31 02:34:36
【问题描述】:

我有 2 个实体,Reply 和 Post。

这些链接作为多对多链接,其中 Post 是所有者。

我已经为回复创建了一个表单来添加新的回复到帖子,但由于某种原因,回复没有显示在 Twig 的 for 循环中。

在数据库中列出并保存了新回复但它没有显示?

我已经设置了将回复链接到帖子的固定装置,它在 for 循环中显示得很好,只是不适用于在表单中创建的新回复?

我在这里错过了什么?

回复表格

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('author')
        ->add('body')
        ->add('post', 'submit');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\DemoBundle\Entity\Reply'
    ));
}

public function getName()
{
    return 'acme_demobundle_reply';
}

树枝

{% for reply in post.replies %}
    <hr>
    <p><small>Reply from <em>{{ reply.author.name }}</em> on {{ reply.createdAt|date }}</small></p>
    <p>{{ reply.body }}</p>

{% endfor %}

控制器

public function createReplyAction(Request $request, $slug)
{
    $post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
        ->findOneBy(array(
           'slug' => $slug
        ));

    if (null == $post) {
        throw $this->createNotFoundException('Post was not found');
    }

    $reply = new Reply();
    $reply->addPost($post);

    $form = $this->createForm(new ReplyType(), $reply);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $this->getDoctrine()->getManager()->persist($reply);
        $this->getDoctrine()->getManager()->flush();

        return $this->redirect($this->generateUrl('acme_core_post_show', array(
            'slug' => $slug
        )));
    }

    return array(
        'post' => $post,
        'form' => $form->createView()
    );
}

回复实体

/**
 * @ORM\ManyToMany(targetEntity="Post", mappedBy="replies")
 */
protected $post;

/**
 * Constructor
 */
public function __construct()
{
    $this->post = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add post
 *
 * @param \Acme\DemoBundle\Entity\Post $post
 * @return Reply
 */
public function addPost(\Acme\DemoBundle\Entity\Post $post)
{
    $this->post[] = $post;

    return $this;
}

/**
 * Remove post
 *
 * @param \Acme\DemoBundle\Entity\Post $post
 */
public function removePost(\Acme\DemoBundle\Entity\Post $post)
{
    $this->post->removeElement($post);
}

/**
 * Get post
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPost()
{
    return $this->post;
}

发布实体

/**
 * @return Array Collection
 *
 * @ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
 * @JoinTable(name="posts_replies",
 *      joinColumns={@JoinColumn(name="post_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="reply_id", referencedColumnName="id")}
 *      )
 */
protected $replies;

/**
 * Constructor
 */
public function __construct()
{
    $this->replies = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add replies
 *
 * @param \Acme\DemoBundle\Entity\Reply $replies
 * @return Post
 */
public function addReply(\Acme\DemoBundle\Entity\Reply $replies)
{
    $replies->addPost($this);

    $this->replies[] = $replies;

    return $this;
}

/**
 * Remove replies
 *
 * @param \Acme\DemoBundle\Entity\Reply $replies
 */
public function removeReply(\Acme\DemoBundle\Entity\Reply $replies)
{
    $this->replies->removeElement($replies);
}

/**
 * Get replies
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getReplies()
{
    return $this->replies;
}

【问题讨论】:

    标签: forms symfony post many-to-many reply


    【解决方案1】:

    删除以下内容:

    $replies-&gt;addPost($this); 来自 Post 实体

    并添加

    $post-&gt;addReply($this); 在 addPost 下用于回复实体。

    【讨论】:

      【解决方案2】:

      您不应该在您的帖子实体中将您的$replies 属性构造为ArrayCollection 吗?

      public function __construct()
      {
           $this->replies = new \Doctrine\Common\Collections\ArrayCollection();
      }
      

      【讨论】:

      • 抱歉,没有粘贴,但确实有。更新。我认为我的表单设置方式有问题?我的意思是当我添加 $post1-&gt;addReply($this-&gt;getReference('reply-1')); 时它可以工作,但当我从这个表单添加时它不起作用......?
      【解决方案3】:

      实体Post的注解错误,试试这个:

       /**
       * @ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
       * @ORM\JoinTable(name="posts_replies",
       *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="reply_id", referencedColumnName="id")}
       *      )
       */
      private $replies;
      

      【讨论】:

      • 不,那没用。随着学说映射的更改,回复仍未出现。我使用的学说映射来自学说页面:http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html (5.15. Many-To-Many, Self-referencing)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2014-02-06
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多