2017-07-28 114 views
0

我是symfony中的新成員,我想知道如何從我的Comment表中呈現作者的用戶名。我只是不知道如何使用該鍵來呈現來自另一個表的值。另外,如果你看到我應該改變的東西,我很欣賞它。如果有人能給我一個例子,那會很棒!Symfony呈現作者姓名

錯誤/樹枝:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies\__CG__\AppBundle\Entity\User could not be converted to string"). 

<div class="panel panel-default"> 
         <div class="panel-heading"> 
          <strong>{{ item.author }}</strong> <span class="text-muted">{{ item.publishedAt|date('Y-m-d H:i:s') }}</span> 
          {% if is_granted('ROLE_USER') %} 
           <a class="btn btn-primary" href="{{ path('comment_comment_new', {'productId': data.id, 'commentId': item.id}) }}">Add Comment</a> 
          {% endif %} 
         </div> 
         <div class="panel-body"> 
          {{ item.content }} 
         </div> 
        </div> 

用戶實體:

/** 
* @ORM\Entity 
* @ORM\Table(name="user") 
* @UniqueEntity(fields={"email"}, message="It looks like your already have an account!") 
*/ 
class User implements UserInterface 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    */ 
    private $id; 

    /** 
    * @Assert\NotBlank() 
    * @Assert\Email() 
    * @ORM\Column(type="string", unique=true) 
    */ 
    private $email; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    private $password; 

    /** 
    * @Assert\NotBlank(groups={"Registration"}) 
    * @var string 
    */ 
    private $plainPassword; 

    /** 
    * @ORM\Column(type="json_array") 
    */ 
    private $roles = array(); 

    /** 
    * @ORM\OneToMany(targetEntity="Comment", mappedBy="author") 
    */ 
    private $user; 

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

    public function getUsername() 
    { 
     return $this->email; 
    } 

    public function getRoles() 
    { 
     $roles = $this->roles; 

     if (!in_array('ROLE_USER', $roles)) 
     { 
      $roles[] = 'ROLE_USER'; 
     } 

     return $roles; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    public function getPassword() 
    { 
     return $this->password; 
    } 

    public function getSalt() 
    { 
    } 

    public function getEmail() 
    { 
     return $this->email; 
    } 

    public function eraseCredentials() 
    { 
     $this->plainPassword = null; 
    } 

    /** 
    * @param mixed $email 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 
    } 

    /** 
    * @param mixed $password 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getPlainPassword() 
    { 
     return $this->plainPassword; 
    } 

    /** 
    * @param mixed $plainPassword 
    */ 
    public function setPlainPassword($plainPassword) 
    { 
     $this->plainPassword = $plainPassword; 
     $this->password = null; 
    } 

    /** 
    * @param mixed $roles 
    */ 
    public function setRoles($roles) 
    { 
     $this->roles = $roles; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 

} 

評論實體:

/** 
* @ORM\Entity(repositoryClass="AppBundle\Repository\CommentRepository") 
* @ORM\Table(name="comments") 
*/ 
class Comment 
{ 
    public function __construct() 
    { 
     $this->publishedAt = new \DateTime(); 
    } 

    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var Product 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $product; 

    /** 
    * @var string 
    * 
    * @ORM\Column(type="text") 
    * @Assert\Length(
    *  min=5, 
    *  minMessage="Comment is too short!", 
    *  max=10000, 
    *  maxMessage="Comment is too long!" 
    *) 
    */ 
    private $content; 

    /** 
    * @var User 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="user") 
    * @ORM\JoinColumn(name="author_id", referencedColumnName="id") 
    */ 
    private $author; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(type="datetime") 
    * @Assert\DateTime 
    */ 
    private $publishedAt; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Comment") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $comment; 

    /** 
    * @return mixed 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @param mixed $id 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 
    } 

    /** 
    * @return Product 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 

    /** 
    * @param Product $product 
    */ 
    public function setProduct($product) 
    { 
     $this->product = $product; 
    } 

    /** 
    * @return string 
    */ 
    public function getContent() 
    { 
     return $this->content; 
    } 

    /** 
    * @param string $content 
    */ 
    public function setContent($content) 
    { 
     $this->content = $content; 
    } 

    /** 
    * @return User 
    */ 
    public function getAuthor() 
    { 
     return $this->author; 
    } 

    /** 
    * @param User $author 
    */ 
    public function setAuthor($author) 
    { 
     $this->author = $author; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getPublishedAt() 
    { 
     return $this->publishedAt; 
    } 

    /** 
    * @param \DateTime $publishedAt 
    */ 
    public function setPublishedAt($publishedAt) 
    { 
     $this->publishedAt = $publishedAt; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getCommentlist() 
    { 
     return $this->comment; 
    } 

    /** 
    * @param mixed $commentlist 
    */ 
    public function setCommentlist($comment) 
    { 
     $this->comment = $comment; 
    } 

} 
+0

你能否提供你正在試圖呈現模板。該錯誤很可能存在(在呈現模板時'Entity \ User無法轉換爲字符串')。 – Michel

回答

1

{{ item.author }}是用戶對象

指定使用者你想屬性顯示:

例:

{{ item.author.username }} 
+0

哦,謝謝你,正是我在找的! –