不可能在一個空可變訪問屬性( 「名稱」)在 消息\ index.html.twig在管線11Symfony2的實體多對一
{{message.author.name}} - 正在
{{message.recipient.name}} - 是不,我不知道爲什麼一切都與筆者一樣
TWIG:
Nadawca: {{ message.author.name }} {{ message.author.surname }}
Odbiorca: {{ message.recipient.name }} {{ message.recipient.surname }}
控制器:
實體用戶:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $name;
/**
* @ORM\Column(type="string", length=100)
*/
private $surname;
/**
* @ORM\OneToMany(targetEntity="Articles", mappedBy="author")
*/
private $articles;
/**
* @ORM\OneToMany(targetEntity="Messages", mappedBy="author")
*/
private $messages_author;
/**
* @ORM\OneToMany(targetEntity="Messages", mappedBy="recipient")
*/
private $messages_recipient;
public function __construct()
{
parent::__construct();
$this->messages_recipient = new ArrayCollection();
$this->messages_author = new ArrayCollection();
$this->articles = new ArrayCollection();
}
實體消息:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $title;
/**
* @ORM\Column(type="text", length=16777216)
*/
private $text;
/**
* @ORM\Column(type="integer", options={"default" = 1})
*/
private $published;
/**
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="messages_author")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="messages_recipient")
* @ORM\JoinColumn(name="recipient_id", referencedColumnName="id")
*/
private $recipient;
public function __construct()
{
$this->date = new \DateTime("now");
}
嘗試在你的樹枝模板中做一個「{{dump(message)}}」。 – Letsrocks