forms - Symfony2 - Entities not linking via ManyToMany -
i have 2 entities, reply , post.
these linked manytomany post being owner.
i have created form reply add new replies post, reason replies not showing in loop in twig.
in database new replies listed , saved yet it's not displaying?
i've setup fixtures linking replies posts , displays fine in loop, not new replies created in form?
what 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
{% reply in post.replies %} <hr> <p><small>reply <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 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); } /** * 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); } /** * replies * * @return \doctrine\common\collections\collection */ public function getreplies() { return $this->replies; }
remove following:
$replies->addpost($this);
post entity
and add in
$post->addreply($this);
under addpost reply entity.
Comments
Post a Comment