繁体   English   中英

当我尝试从树枝模板访问它时,对象 USER 实体为空

[英]object USER entity is null when i try to access it from twig template

我无法通过主题实体访问用户实体。

在模板树枝中,我使用转储来显示结果查询...:

{% for forum in listForums %}
    {{ dump (forum.topics.last)}}
    ....

我们可以看到 User 实体为空(null):

Topic {#1456 ▼

  -Forum: Forum {#435 ▶}
  -Posts: PersistentCollection {#1457 ▶}
  -id: 65
  -title: "How to go over there"
  -User: User {#1294 ▼

    -_entityPersister: BasicEntityPersister {#1291 ▶}
    -_identifier: array:1 [▶]
    +__isInitialized__: false
    -Topics: null
    #id: null
    -pseudo: null
    -name: null
    -firstname: null
    -website: null
    -avatar: null
    -signature: null
    -location: null
    -registration: null
    -lastVisit: null
    -rank: null
    -nbPost: null
    -nbTopic: null
    #username: null
    #usernameCanonical: null
    #email: null
    #emailCanonical: null
    #enabled: null
    #salt: null
    #password: null
    #plainPassword: null
    #lastLogin: null
    #confirmationToken: null
    #passwordRequestedAt: null
    #groups: null
    #locked: null
    #expired: null
    #expiresAt: null
    #roles: null
    #credentialsExpired: null
    #credentialsExpireAt: null

}
  -viewCount: 23
  -dateCreation: DateTime {#1455 ▶}
  -replyCount: 123
  -slug: "slug_sluggg"
  -genre: "genre"
  -lastPost: 25
  -content: """
    <p>test</p>\r\n
    \r\n
    <p>test2</p>\r\n
    \r\n
    <p>test3</p>
    """
}

控制器的一部分

class FController extends Controller
{

    public function indexAction()
    {

        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();

        $listForums = $em->getRepository('BISSAPForumBundle:Forum')->findAllOrderByCategory();

        return $this->render('BISSAPForumBundle:F:index-forum.html.twig', array('listForums' => $listForums, 'user' => $user));
    }
}

主题.php

<?php

namespace BISSAP\ForumBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/*use BISSAP\BodyConcept\Entity\Forum;
*/

/**
 * Topic
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\TopicRepository")
 */
class Topic
{
    /**
     * @ORM\ManyToOne(targetEntity="Forum", inversedBy="Topics", cascade={"persist"})
     * @ORM\JoinColumn(name="forum_id", referencedColumnName="id")
     */
    private $Forum;

    /**
     * @var ArrayCollection $Posts
     *
     * @ORM\OneToMany(targetEntity="Post", mappedBy="Topic", cascade={"persist", "remove", "merge"})
     */
    private $Posts;

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

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @ORM\ManyToOne(targetEntity="BISSAP\UserBundle\Entity\User", inversedBy="Topics", cascade={"persist"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $User;

    /**
     * @var integer
     *
     * @ORM\Column(name="view_count", type="integer")
     */
    private $viewCount;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_creation", type="datetime")
     */
    private $dateCreation;

    /**
     * @var integer
     *
     * @ORM\Column(name="reply_count", type="integer")
     */
    private $replyCount;

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255)
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="genre", type="string", length=255)
     */
    private $genre;

    /**
     * @var integer
     *
     * @ORM\Column(name="last_post", type="integer")
     */
    private $lastPost;

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


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

    /**
     * Set title
     *
     * @param string $title
     * @return Topic
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set user
     *
     * @param integer $user
     * @return Topic
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

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

    /**
     * Set viewCount
     *
     * @param integer $viewCount
     * @return Topic
     */
    public function setViewCount($viewCount)
    {
        $this->viewCount = $viewCount;

        return $this;
    }

    /**
     * Get viewCount
     *
     * @return integer 
     */
    public function getViewCount()
    {
        return $this->viewCount;
    }

    /**
     * Set dateCreation
     *
     * @param \DateTime $dateCreation
     * @return Topic
     */
    public function setDateCreation($dateCreation)
    {
        $this->dateCreation = $dateCreation;

        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return \DateTime 
     */
    public function getDateCreation()
    {
        return $this->dateCreation;
    }

    /**
     * Set replyCount
     *
     * @param integer $replyCount
     * @return Topic
     */
    public function setReplyCount($replyCount)
    {
        $this->replyCount = $replyCount;

        return $this;
    }

    /**
     * Get replyCount
     *
     * @return integer 
     */
    public function getReplyCount()
    {
        return $this->replyCount;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Topic
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set genre
     *
     * @param string $genre
     * @return Topic
     */
    public function setGenre($genre)
    {
        $this->genre = $genre;

        return $this;
    }

    /**
     * Get genre
     *
     * @return string 
     */
    public function getGenre()
    {
        return $this->genre;
    }

    /**
     * Set lastPost
     *
     * @param integer $lastPost
     * @return Topic
     */
    public function setLastPost($lastPost)
    {
        $this->lastPost = $lastPost;

        return $this;
    }

    /**
     * Get lastPost
     *
     * @return integer 
     */
    public function getLastPost()
    {
        return $this->lastPost;
    }

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

        return $this;
    }

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

    /**
     * Set forum
     *
     * @param Forum $forum
     * @return Topic
     */
    /*public function setForum(\BISSAP\BodyConceptBundle\Entity\Forum $forum)*/
    public function setForum(Forum $forum)
    {
        $this->forum = $forum;

        return $this;
    }

    /**
     * Get forum
     *
     * @return \BISSAP\BodyConceptBundle\Entity\Forum 
     */
    public function getForum()
    {
        return $this->forum;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
    $this->dateCreation = new \DateTime();
    }

    /**
     * Add posts
     *
     * @param \BISSAP\ForumBundle\Entity\Post $posts
     * @return Topic
     */
    public function addPost(\BISSAP\ForumBundle\Entity\Post $posts)
    {
        $this->posts[] = $posts;

        return $this;
    }

    /**
     * Remove posts
     *
     * @param \BISSAP\ForumBundle\Entity\Post $posts
     */
    public function removePost(\BISSAP\ForumBundle\Entity\Post $posts)
    {
        $this->posts->removeElement($posts);
    }

    /**
     * Get posts
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPosts()
    {
        return $this->posts;
    }
}

用户名

<?php

namespace BISSAP\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * BISSAP\UserBundle\Entity\User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BISSAP\UserBundle\Entity\UserRepository")
 */
class User extends BaseUser
{
    /**
     * @var ArrayCollection $Topics
     *
     * @ORM\OneToMany(targetEntity="\BISSAP\ForumBundle\Entity\Topic", mappedBy="User", cascade={"persist", "remove", "merge"})
     */
    private $Topics;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="pseudo", type="string", length=30, nullable=true)
     */
    private $pseudo;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $name;

     /**
     * @ORM\Column(name="firstname", type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your firstname.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="website", type="string", length=100, nullable=true)
     */
    private $website;

    /**
     * @var string
     *
     * @ORM\Column(name="avatar", type="string", length=100, nullable=true)
     */
    private $avatar;

    /**
     * @var string
     *
     * @ORM\Column(name="signature", type="string", length=200, nullable=true)
     */
    private $signature;

    /**
     * @var string
     *
     * @ORM\Column(name="location", type="string", length=100, nullable=true)
     */
    private $location;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="registration", type="datetime")
     */
    private $registration;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_visit", type="datetime")
     */
    private $lastVisit;

    /**
     * @var integer
     *
     * @ORM\Column(name="rank", type="integer", nullable=true)
     */
    private $rank;

    /**
     * @var integer
     *
     * @ORM\Column(name="nb_post", type="integer", nullable=true)
     */
    private $nbPost;

    /**
     * @var integer
     *
     * @ORM\Column(name="nb_topic", type="integer", nullable=true)
     */
    private $nbTopic;


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

    /**
     * Set pseudo
     *
     * @param string $pseudo
     * @return User
     */
    public function setPseudo($pseudo)
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    /**
     * Get pseudo
     *
     * @return string 
     */
    public function getPseudo()
    {
        return $this->pseudo;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set website
     *
     * @param string $website
     * @return User
     */
    public function setWebsite($website)
    {
        $this->website = $website;

        return $this;
    }

    /**
     * Get website
     *
     * @return string 
     */
    public function getWebsite()
    {
        return $this->website;
    }

    /**
     * Set avatar
     *
     * @param string $avatar
     * @return User
     */
    public function setAvatar($avatar)
    {
        $this->avatar = $avatar;

        return $this;
    }

    /**
     * Get avatar
     *
     * @return string 
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * Set signature
     *
     * @param string $signature
     * @return User
     */
    public function setSignature($signature)
    {
        $this->signature = $signature;

        return $this;
    }

    /**
     * Get signature
     *
     * @return string 
     */
    public function getSignature()
    {
        return $this->signature;
    }

    /**
     * Set location
     *
     * @param string $location
     * @return User
     */
    public function setLocation($location)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return string 
     */
    public function getLocation()
    {
        return $this->location;
    }

    /**
     * Set registration
     *
     * @param \DateTime $registration
     * @return User
     */
    public function setRegistration($registration)
    {
        $this->registration = $registration;

        return $this;
    }

    /**
     * Get registration
     *
     * @return \DateTime 
     */
    public function getRegistration()
    {
        return $this->registration;
    }

    /**
     * Set lastVisit
     *
     * @param \DateTime $lastVisit
     * @return User
     */
    public function setLastVisit($lastVisit)
    {
        $this->lastVisit = $lastVisit;

        return $this;
    }

    /**
     * Get lastVisit
     *
     * @return \DateTime 
     */
    public function getLastVisit()
    {
        return $this->lastVisit;
    }

    /**
     * Set rank
     *
     * @param integer $rank
     * @return User
     */
    public function setRank($rank)
    {
        $this->rank = $rank;

        return $this;
    }

    /**
     * Get rank
     *
     * @return integer 
     */
    public function getRank()
    {
        return $this->rank;
    }

    /**
     * Set nbPost
     *
     * @param integer $nbPost
     * @return User
     */
    public function setNbPost($nbPost)
    {
        $this->nbPost = $nbPost;

        return $this;
    }

    /**
     * Get nbPost
     *
     * @return integer 
     */
    public function getNbPost()
    {
        return $this->nbPost;
    }

    /**
     * Set nbTopic
     *
     * @param integer $nbTopic
     * @return User
     */
    public function setNbTopic($nbTopic)
    {
        $this->nbTopic = $nbTopic;

        return $this;
    }

    /**
     * Get nbTopic
     *
     * @return integer 
     */
    public function getNbTopic()
    {
        return $this->nbTopic;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
    parent::__construct();
        $this->Topics = new \Doctrine\Common\Collections\ArrayCollection();
    $this->registration = new \DateTime();
    $this->lastVisit = new \DateTime();

    }

    /**
     * Add Topics
     *
     * @param \BISSAP\ForumBundle\Entity\Topic $topics
     * @return User
     */
    public function addTopic(\BISSAP\ForumBundle\Entity\Topic $topics)
    {
        $this->Topics[] = $topics;

        return $this;
    }

    /**
     * Remove Topics
     *
     * @param \BISSAP\ForumBundle\Entity\Topic $topics
     */
    public function removeTopic(\BISSAP\ForumBundle\Entity\Topic $topics)
    {
        $this->Topics->removeElement($topics);
    }

    /**
     * Get Topics
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getTopics()
    {
        return $this->Topics;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return User
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

    /**
     * Get firstname
     *
     * @return string 
     */
    public function getFirstname()
    {
        return $this->firstname;
    }
}

我想查看 Doctrine 生成的请求,但我不知道如何显示它。


所以我试图用{{ forum.topics.last.user.pseudo }}访问伪用户

我得到错误:

An exception has been thrown during the rendering of a template ("Notice: Undefined property: BISSAP\ForumBundle\Entity\Topic::$user") in BISSAPForumBundle:F:index-forum.html.twig at line 53. 

我通过 $user 移动了 $User,我得到:

Impossible to access an attribute ("user") on a boolean variable ("") in BISSAPForumBundle:F:index-forum.html.twig at line 53 

UserRepository::findAllOrderByCategory() 不存在但

ForumRepository::findAllOrderByCategory() :

class ForumRepository extends EntityRepository
{
    public function findAllOrderByCategory()
    {
        return $this->createQueryBuilder('p')
                    ->leftJoin('p.Category','c')
                    ->orderBy('c.ordre', 'desc')
                    ->getQuery()
                    ->getResult();
    }
}

Doctrine 默认是延迟加载相关对象,除非相关对象已经加载。 访问相关对象(即回显 Topic.user.pseudo)应触发加载完整对象。 请参阅以下脚注: http : //doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins

如果这不起作用,您能否在响应中添加错误消息的屏幕截图和 UserRepository::findAllOrderByCategory() 方法的内容?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM