Symfony2实体ManyToOne

时间:2016-05-25 11:10:13

标签: symfony doctrine-orm twig entity

  

无法访问null变量中的属性(“name”)   第11行的messages \ index.html.twig

{{message.author.name}} - 正在运作

{{message.recipient.name}} - 是不是,我不知道为什么一切与作者相同

TWIG:

Nadawca: {{ message.author.name }} {{ message.author.surname }}
Odbiorca: {{ message.recipient.name }} {{ message.recipient.surname }}

控制器:

public function indexAction() {
    $limit = 100;
    $em = $this->getDoctrine()->getManager();
    $user_data = $this->get('security.token_storage')->getToken()->getUser();
    $repository = $this->getDoctrine()->getRepository('AppBundle:Messages');
    $query = $repository->createQueryBuilder('m')->where('m.author = :authorid')
                                                 ->orWhere('m.recipient = :authorid')
                                                 ->setParameter('authorid', $user_data->getId())
                                                 ->getQuery();
    $messages = $query->getResult();
    return $this->render('messages/index.html.twig', array(
        'messages' => $messages,
        'user' => $user_data,
    ));
}

实体用户:

 /**
 * @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");

}

1 个答案:

答案 0 :(得分:0)

您的错误意味着一条或多条消息中没有收件人。查看从数据库中检索的所有邮件,并检查其中是否有任何收件人为NULL。或者使用简单的/** * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Video") * @ORM\JoinColumn(name="video_id", referencedColumnName="id") */ private $videoId; 快速查询数据库。

这是由于延迟加载。首先,Message实体中的SELECT * FROM message WHERE recipient_id IS NULLauthor都是空的或Proxy对象。一旦访问该属性,doctrine将尝试将关联的用户实体加载到Message中。因此,当您调用recipient并且您的{{ message.recipient.name }}实际上没有返回User实体时,它将保持为NULL,从而导致错误,指出它无法访问NULL上的属性message.recipient