我希望user
能够在其公共配置文件中留下关于user
的评论(评论)。
一旦我刷新公共配置文件页面,我有这个错误:
在呈现模板期间抛出异常("控制器"飞行控制器 BookingsBundle 为PostController: commentAction ()要求您为"$entity"参数提供一个值(因为没有默认值或因为有一个非可选参数在这个之后)ApplicationSonataUserBundle:简介:UserShow.html。修改第108行
公共配置文件的url如下所示:
http://127.0.0.1/symfony/web/app_dev.php/user/show/john/26
Routing.yml
userShow:
pattern: /user/show/{entity}/{slug}
defaults: { _controller: FLYBookingsBundle:Post:userShow }
comment:
pattern: /user/show/{entity}/{slug}
defaults: { _controller: FLYBookingsBundle:Post:comment }
requirements:
_method: POST
PostController.php
public function userShowAction($entity)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApplicationSonataUserBundle:User')->findBy(array('username' => $entity));
$user = $em->getRepository('ApplicationSonataUserBundle:Avatar')->findBy(array('user' => $entity));
return $this->render('ApplicationSonataUserBundle:Profile:UserShow.html.twig', array('user' => $user,'entity' => $entity));
}
.
public function commentAction(Request $request,$entity)
{
$em = $this->getDoctrine()->getManager();
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$comment = new Comment();
$entity = $em->getRepository('ApplicationSonataUserBundle:User')->findBy(array('username' => $entity));
$form = $this->createForm( new CommentType(),$comment);
dump($form);
if ($this->get('request')->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment->setCreatedAt(new DateTime());
$comment->setApproved(true);
$comment->setRecipient();
$comment->setAuthor($this->container->get('security.token_storage')->getToken()->getUser());
$em->persist($comment);
$em->flush();
$this->get('session')->getFlashBag()->add(
'success',
'Your comment was succesfully added'
);
}
return $this->redirect($this->generateUrl('userShow', array(
'entity' => $entity,
//'slug' => $slug,
)));
}
return $this->render('ApplicationSonataUserBundle:Profile:commentForm.html.twig', array( 'entity' => $entity,'user' => $user,'comment' => $comment,
'form' => $form->createView()));
}
UserShow.html.twig
{% for entity in entity %}
<div class="row">
<div class="col-sm-9">
{{ render(controller('FLYBookingsBundle:Post:comment')) }}
</div>
</div>
{% endfor %}
CommentForm.html.twig
<form action="{{ path('comment', {'entity': entity, 'slug':entity.user.id}) }}" method="POST">
{{ form_start(form) }}
{{ form_errors(form) }}
<div class="form-group">
<label>Review Text</label>
{{ form_widget(form.body, { 'attr': {'class': 'form-control','style': 'width: 100%', } }) }}
{{ form_errors(form.body) }}
</div>
<button style="float: right;font-size: 14px; height: 40px; width: 180px;" type="submit">Leave a Review</button>
{{ form_rest(form) }}
</form>
添加:
public function userShowAction(Request $request,$entity,$slug)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApplicationSonataUserBundle:User')->findBy(array('username' => $entity));
$useravatar = $em->getRepository('ApplicationSonataUserBundle:Avatar')->findBy(array('user' => $entity));
$recipient = $em->getRepository('ApplicationSonataUserBundle:User')->findOneBy(array('id' => $slug));
$comment = new Comment();
$user = new User();
$form = $this->createForm( new CommentType(),$comment);
if ($this->get('request')->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment->setCreatedAt(new DateTime());
$comment->setApproved(true);
$comment->setRecipient($recipient);
dump($entity);
$comment->setAuthor($this->container->get('security.token_storage')->getToken()->getUser());
$em->persist($comment);
$em->flush();
$this->get('session')->getFlashBag()->add(
'success',
'Your comment was succesfully added'
);
}
return $this->redirect($this->generateUrl('userShow', array(
'entity' => $comment->getRecipient(),
'slug' => $slug
)));
}
return $this->render('ApplicationSonataUserBundle:Profile:UserShow.html.twig', array('useravatar' => $useravatar,'user' => $user,'entity' => $entity,'form' => $form->createView()));
}
.
Comment.php
<?php
namespace ApplicationSonataUserBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsCollection;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMHasLifecycleCallbacks
* @ORMEntity
* @ORMEntity(repositoryClass="ApplicationSonataUserBundleEntityUserRepository")
* @ORMTable(name="comment")
*
*/
class Comment
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var User
* @ORMManyToOne(targetEntity="ApplicationSonataUserBundleEntityUser", inversedBy="commentrecipient")
* @ORMJoinColumn(onDelete="CASCADE")
*
*/
protected $recipient;
/**
* @var User
* @ORMManyToOne(targetEntity="ApplicationSonataUserBundleEntityUser", inversedBy="comment")
*/
protected $author;
/**
* @ORMColumn(name="approved",type="boolean", nullable=true)
*/
protected $approved;
/**
* @var string
*
* @ORMColumn(name="body", type="text")
* @AssertNotBlank
*/
private $body;
/**
* @ORMColumn(name="createdAt", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* @ORMColumn(name="updatedAt", type="datetime", nullable=false)
*/
protected $updatedAt;
public function __toString()
{
return (string) $this->getRecipient();
}
public function __construct()
{
$this->setCreatedAt(new DateTime());
$this->setUpdatedAt(new DateTime());
$this->setApproved(true);
}
}
User.php
/**
* @ORMOneToMany(targetEntity="ApplicationSonataUserBundleEntityComment", mappedBy="recipient", cascade={"persist"})
* @ORMJoinColumn(nullable=true)
*/
protected $commentrecipient;
FLYBookingsBundle:Post:comment
要求参数
将{{ render(controller('FLYBookingsBundle:Post:comment')) }}
更改为
{{ render(controller('FLYBookingsBundle:Post:comment', {'entity' : entity )) }}
在<<p> strong> UserShow.html.twig