0

I have 2 entities, Reply and Post.

These are linked as ManyToMany with Post being the owner.

I have created a form for Reply to add new replies to the post, but the for some reason the replies are not showing up in the for loop in Twig.

In the database the new replies are listed and saved yet it's not displaying?

I've setup fixtures for linking replies to posts and it displays just fine in the for loop, just not for the new replies created in the form?

What am I missing here?

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

Twig

{% 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 %}

Controller

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

Reply entity

/**
 * @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;
}

Post entity

/**
 * @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;
}
dizzyd
  • 201
  • 1
  • 7
  • 15

3 Answers3

1

Remove the following:

$replies->addPost($this); from Post entity

and add in

$post->addReply($this); under addPost for Reply entity.

webdev
  • 741
  • 5
  • 16
0

Shouldn't you construct your $replies property as an ArrayCollection in your Post Entity?

public function __construct()
{
     $this->replies = new \Doctrine\Common\Collections\ArrayCollection();
}
Debreczeni András
  • 1,597
  • 10
  • 18
  • Sorry, didn't paste that in, but it does have that. Updated. I think there is something wrong with the way my form is setup? I mean it works when I add `$post1->addReply($this->getReference('reply-1'));` but just not when added from this form...? – dizzyd May 31 '14 at 22:14
-1

the entity Post's annotation is wrong, try this one:

 /**
 * @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;
Nam Luuduc
  • 19
  • 3
  • No, that didn't work. The replies are still not appearing with the change to the doctrine mapping. The doctrine mapping I am using is from the doctrine page: `http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html (5.15. Many-To-Many, Self-referencing)` – dizzyd May 31 '14 at 08:34