에 숨겨진 필드를 만들려면 폼 클래스에 변수를 전달합니다. 나는 각각의 실체에서 게시물과 코멘트 사이에 확립 된 일대 다 관계를 맺고있다. 게시물이 홈 페이지에 표시되면 사용자가 의견을 게시 할 수있는 주석 양식이 필요합니다. 코멘트를 작성하기 위해, 나는 postID의 형태로 숨겨진 필드를 통과해야하지만 문제가 그렇게를 데. 다음은 내 코드입니다 ..심포니 2 동적으로 게시물이 많은 의견이 있습니다 내가 심포니 2 프레임 워크 도구의 블로그 일종의 일하고 형태
컨트롤러 : 여기
<?php
namespace Issh\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Issh\MainBundle\Form\Post\CommentForm;
use Issh\MainBundle\Form\Post\PostForm;
use Issh\MainBundle\Entity\IsshPost;
use Issh\MainBundle\Entity\IsshComment;
class HomeController extends Controller
{
public function indexAction()
{
return $this->render('IsshMainBundle:Home:index.html.php');
}
public function postAction()
{
$em = $this->get('doctrine')->getEntityManager();
$request = $this->get('request');
$post = new IsshPost();
$form = $this->createForm(new PostForm(), $post);
if ('POST' == $request->getMethod())
{
$form->bindRequest($request);
$post->setIsshUser($this->get('security.context')->getToken()->getUser());
if ($form->isValid())
{
$em->persist($post);
$em->flush();
return $this->redirect($this->generateUrl('home'));
}
return $this->render('IsshMainBundle:Home:IsshPost.html.php', array(
'form' => $form->createView()));
}
else
{
$em = $this->getDoctrine()->getEntityManager();
$posts = $em->getRepository('IsshMainBundle:IsshPost')->getLatestPosts();
return $this->render('IsshMainBundle:Home:IsshPost.html.php', array(
'form' => $form->createView(), 'posts' => $posts));
}
}
public function commentAction($postID = null)
{
$em = $this->get('doctrine')->getEntityManager();
$request = $this->get('request');
$comment = new IsshComment();
$form = $this->createForm(new CommentForm($postID), $comment); // need to pass postID here so it can be set as hidden field
if ('POST' == $request->getMethod())
{
$form->bindRequest($request);
$comment->setIsshUser($this->get('security.context')->getToken()->getUser());
if ($form->isValid()) {
$em->persist($comment);
$em->flush();
return $this->redirect($this->generateUrl('home'));
}
}
else
{
return $this->render('IsshMainBundle:Home:IsshComment.html.php', array(
'form' => $form->createView()));
}
}
}
입니다 포스트 양식 :
<?php
namespace Issh\MainBundle\Form\Post;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PostForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('text','text');
}
public function getName()
{
return 'postForm';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Issh\MainBundle\Entity\IsshPost'
);
}
}
?>
코멘트 양식은 (아직 작동하지 않습니다) :
<?php
namespace Issh\MainBundle\Form\Post;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class CommentForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('text','text');
}
public function getName()
{
return 'commentForm';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Issh\MainBundle\Entity\IsshComment'
);
}
}
?>
포스트 템플릿 :
(210)주석 템플릿 :
<?php if(!empty($form)): ?>
<form action="<?php echo $view['router']->generate('comment') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
<?php echo $view['form']->errors($form) ?>
<?php echo $view['form']->row($form['text']) ?>
<?php echo $view['form']->rest($form) ?>
<input type="submit" />
</form>
<?php endif; ?>
<?php if(!empty($comments)): ?>
<?php foreach ($comments as $comment): ?>
<p><?php echo $comment->getText();?></p>
--------------------------------------
<?php endforeach; ?>
<?php endif; ?>
누구? 나는 정말로 약간의 도움에 감사 할 것이다! 이 – chintan
"데이터 변환기"함께 거래의 적절한 방법을 붙이게되고, http://symfony.com/doc/current/cookbook/form/data_transformers.html 참조 – KevinS