2016-11-20 54 views
-1

我想让一个页面中有多个帖子(在同一页面中),并且每个帖子都有一个表单并在该表单中输入该表单中的内容,您可以编写关于该帖子的某些内容,然后编写文本和帖子ID被发送到数据库。我如何用symfony做到这一点?Symfony多个表单和输入

+0

这种类型的问题是通常被删除或关闭太宽泛或无关主题(如果您正在寻找免费的编码服务或教程)。您只能阅读文档以了解symfony是如何工作的,然后您将具备编写代码的基本知识,并返回此处以获得与代码相关的特定问题的帮助。另请阅读:[你可以问什么问题](http://stackoverflow.com/help/on-topic)和[我如何问一个好问题?](http://stackoverflow.com/help/how这些问题将帮助你制定一个更好的问题。 –

回答

0

其实很简单。让你的实体和formtype也像你一样使用。诀窍在于将帖子ID粘贴到表单的动作URL中,以便呈现

<form action="/reaction/new/1"> 

其中1是帖子ID。我用枝条控制器功能呈现每个形式与正确的动作URL:

{{ render(controller('AppBundle:Reaction:new', {'postId':post.id})) }} 

发布实体:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Post 
* 
* @ORM\Table(name="post") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository") 
*/ 
class Post 
{ 
    // ... 

    /** 
    * @ORM\OneToMany(targetEntity="Reaction", mappedBy="post") 
    */ 
    private $reactions; 

    public function __construct() 
    { 
     $this->reactions = new ArrayCollection(); 
    } 

    // ... 
} 

反应Enity:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Reaction 
* 
* @ORM\Table(name="reaction") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\ReactionRepository") 
*/ 
class Reaction 
{ 
    // ... 

    /** 
    * @ORM\ManyToOne(targetEntity="Post", inversedBy="reactions") 
    * @ORM\JoinColumn(name="post_id", referencedColumnName="id") 
    */ 
    private $post; 

    // ... 
} 

后控制器:

namespace AppBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

/** 
* Post controller. 
* 
* @Route("post") 
*/ 
class PostController extends Controller 
{ 
    /** 
    * @Route("/", name="post_index") 
    */ 
    public function indexAction() 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $posts = $em->getRepository('AppBundle:Post')->findAll(); 

     return $this->render('post/index.html.twig', array(
      'posts' => $posts, 
     )); 
    } 
} 

反应控制器:

namespace AppBundle\Controller; 

use AppBundle\Entity\Reaction; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request; 

/** 
* Reaction controller. 
* 
* @Route("reaction") 
*/ 
class ReactionController extends Controller 
{ 
    /** 
    * Creates a new reaction entity. 
    * 
    * @Route("/new/{postId}", name="reaction_new") 
    * @Method({"GET", "POST"}) 
    */ 
    public function newAction(Request $request, $postId) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $reaction = new Reaction(); 
     $form = $this->createForm('AppBundle\Form\ReactionType', $reaction, array(
      /* 
      * You must change the action otherwise we will post to Post:index action ! 
      */ 
      'action' => $this->generateUrl('reaction_new', array('postId' => $postId)) 
     )); 
     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) 
     { 
      /* 
      * load post entity and add it to the reaction::post collection ! 
      */ 
      $post = $em->getRepository('AppBundle:Post')->find($postId); 
      $reaction->setPost($post); 

      $em->persist($reaction); 
      $em->flush($reaction); 

      /* 
      * return to the post page 
      */ 
      return $this->redirectToRoute('post_index'); 
     } 

     return $this->render('reaction/new.html.twig', array(
      'form' => $form->createView(), 
     )); 
    } 
} 

交/ index.html.twig:

{% extends 'base.html.twig' %} 

{% block body %} 
    {% for post in posts %} 
     <h1>{{ post.title }}</h1> 
     <p>{{ post.body }}</p> 
     <h3>Reactions</h3> 
     <ul> 
     {% for reaction in post.reactions %} 
      <li>{{ reaction.message }}</li> 
     {% endfor %} 
     </ul> 
     {{ render(controller('AppBundle:Reaction:new', {'postId':post.id})) }} 
    {% endfor %} 
{% endblock %} 

反应/ new.html.twig:

{{ form_start(form) }} 
    {{ form_widget(form) }} 
    <input type="submit" value="Send" /> 
{{ form_end(form) }}