0

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

这些链接为 ManyToMany,而 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;
}
4

3 回答 3

1

删除以下内容:

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

并添加

$post->addReply($this);在回复实体的 addPost 下。

于 2014-05-31T23:50:05.280 回答
0

你不应该 在你的 Post Entity 中构建你的$replies属性吗?ArrayCollection

public function __construct()
{
     $this->replies = new \Doctrine\Common\Collections\ArrayCollection();
}
于 2014-05-31T15:20:14.730 回答
-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;
于 2014-05-31T06:54:37.067 回答