2016-11-20 13 views
-1

複数の投稿がある(同じページ内にある)ページを作りたいと思います。すべての投稿にフォームがあり、そのフォーム内にそのフォームの中に入力すると、その投稿、テキスト、および投稿IDはデータベースに送られます。 symfonyを使ってどうすればいいですか?symfony複数のフォームと入力

+0

このタイプの質問であります通常は削除されるか、あまりにも広すぎたり話題になったりすることはありません(無料のコーディングサービスやチュートリアルをお探しの場合)。 symfonyがどのように動作するかを知るためにのみドキュメントを読むことができます。そして、コードに関連する特定の問題のための助けを得るために、いくつかのコードを書いてここに戻ります。また、[どのようなトピックをあなたがSOで尋ねることができます](http://stackoverflow.com/help/on-topic)と[どのように私は良い質問をしますか?](http://stackoverflow.com/help/howこれらのトピックは、より良い質問を策定するのに役立ちます。 –

答えて

0

実際は非常に簡単です。エンティティと書式を使用するようにします。トリックはそれが1ポストIDです

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

をレンダリングするように、フォームのアクション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; 

    // ... 
} 

Iは正しいアクションURLと各フォームをレンダリングするために小枝コントローラ機能を使用しましたポストコントローラ:

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) }} 
関連する問題