Symfony2 - 未通过ManyToMany链接的实体

时间:2014-05-31 02:34:37

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

我有2个实体,回复和发布。

这些链接为ManyToMany,Post是所有者。

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

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

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

我在这里缺少什么?

ReplyForm

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

3 个答案:

答案 0 :(得分:1)

删除以下内容:

来自Post实体的

$replies->addPost($this);

并加入

在addPost for Reply实体下的

$post->addReply($this);

答案 1 :(得分:0)

您不应该在邮政实体中将$replies财产构建为ArrayCollection吗?

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

答案 2 :(得分:-1)

实体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;