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