2017-07-28 7 views
0

私はsymfonyの新機能です。コメントテーブルから作者のユーザ名をレンダリングする方法を知りたいと思います。私はちょうど別のテーブルから値をレンダリングするためにそのキーを使用する方法を知りません。また、私が変えなければならない何かを見たら、私はそれを感謝します。誰かが私に例を挙げてもらえれば、それは素晴らしいだろう!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

ああ、ありがとう、ちょうど私が探していたもの! –

関連する問題