2016-10-25 71 views
0

我想user能夠對其公開的個人資料上的user留下評論(評論)。例外「一些強制性參數丟失」[symfony2]

一旦我刷新公開資料頁上我有這樣的錯誤:

An exception has been thrown during the rendering of a template ("Controller "FLY\BookingsBundle\Controller\PostController::commentAction()" requires that you provide a value for the "$entity" argument (because there is no default value or because there is a non optional argument after this one).") in ApplicationSonataUserBundle:Profile:UserShow.html.twig at line 108.

公用配置文件看起來像這樣的網址:

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> 

ADD:

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 Application\Sonata\UserBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\Collection; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\HasLifecycleCallbacks 
* @ORM\Entity 
* @ORM\Entity(repositoryClass="Application\Sonata\UserBundle\Entity\UserRepository") 
* @ORM\Table(name="comment") 
* 
*/ 

class Comment 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var User 
    * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commentrecipient") 
    * @ORM\JoinColumn(onDelete="CASCADE") 
    * 
    */ 
    protected $recipient; 

    /** 
    * @var User 
    * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="comment") 
    */ 
    protected $author; 

    /** 
    * @ORM\Column(name="approved",type="boolean", nullable=true) 
    */ 
    protected $approved; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="body", type="text") 
    * @Assert\NotBlank 
    */ 
    private $body; 

    /** 
    * @ORM\Column(name="createdAt", type="datetime", nullable=false) 
    */ 
    protected $createdAt; 
    /** 
    * @ORM\Column(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的

/** 
* @ORM\OneToMany(targetEntity="Application\Sonata\UserBundle\Entity\Comment", mappedBy="recipient", cascade={"persist"}) 
* @ORM\JoinColumn(nullable=true) 
*/ 
protected $commentrecipient; 

回答

1

FLYBookingsBundle:Post:comment需要的參數。

變化{{ render(controller('FLYBookingsBundle:Post:comment')) }}

{{ render(controller('FLYBookingsBundle:Post:comment', {'entity' : entity)) }} 

UserShow.html.twig

+0

@Ryan文森特。 @Adashbob。今天早上我設法使它工作。我所做的就是不再像以前那樣渲染我的表單了。{{render(controller('FLYBookingsBundle:Post:comment'))}}'。我決定直接在方法userShowAction中插入我的表單。我遇到了另一個問題,我如何獲取收件人的用戶標識?我的網址是這樣的:'http:// 127.0.0.1/symfony/web/app_dev.php/user/show/john/26',你可以在這裏看到'recipient_id'是'26'。 – Sirius

+0

betewen用戶,評論和Recipiend存在什麼樣的關係?如果用戶是通過身份驗證的用戶,您可以通過:$ this-> container-> get('security.token_storage') - > getToken() - > getUser()'來獲取它。 – bobo

+0

如果用戶沒有登錄,則不能發表評論,所以我可以像這樣得到作者:'$ comment-> setAuthor($ this-> container-> get('security.token_storage') - > getToken ) - > getUser());'但我想獲得'recipient_id'。在我的問題上,我添加了我的表格評論和用戶,這樣你就可以看到兩個表格之間的關係。 – Sirius