0

我有这两个实体:

事件:

class Event
    {
        /**
         * @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;

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

        /**
         * @var boolean
         *
         * @ORM\Column(name="invitationPermission", type="boolean")
         */
        private $invitationPermission;

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

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

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


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

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

            return $this;
        }

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

        /**
         * Set description
         *
         * @param string $description
         * @return Event
         */
        public function setDescription($description)
        {
            $this->description = $description;

            return $this;
        }

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

        /**
         * Set invitationPermission
         *
         * @param boolean $invitationPermission
         * @return Event
         */
        public function setInvitationPermission($invitationPermission)
        {
            $this->invitationPermission = $invitationPermission;

            return $this;
        }

        /**
         * Get invitationPermission
         *
         * @return boolean 
         */
        public function getInvitationPermission()
        {
            return $this->invitationPermission;
        }

        /**
         * Set creationDate
         *
         * @param \DateTime $creationDate
         * @return Event
         */
        public function setCreationDate($creationDate)
        {
            $this->creationDate = $creationDate;

            return $this;
        }

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

        /**
         * Set modifiedDate
         *
         * @param \DateTime $modifiedDate
         * @return Event
         */
        public function setModifiedDate($modifiedDate)
        {
            $this->modifiedDate = $modifiedDate;

            return $this;
        }

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

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

            return $this;
        }

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

用户

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

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

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

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


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

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

        return $this;
    }

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

    /**
     * Set nick
     *
     * @param string $nick
     * @return Users
     */
    public function setNick($nick)
    {
        $this->nick = $nick;

        return $this;
    }

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

    /**
     * Set pwd
     *
     * @param string $pwd
     * @return Users
     */
    public function setPwd($pwd)
    {
        $this->pwd = $pwd;

        return $this;
    }

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

我尝试获取一个事件,然后通过事件,获取用户,然后是昵称:

$event = $em->getRepository('MyBundle:Event')->findOneById(1);
print_r($event->getUser()->getNickname());

我得到:

Fatal error: Call to undefined method Doctrine\ORM\PersistentCollection::getNickname() in C:\xampp\htdocs\web\src\KNV\MyBundle\Controller\DefaultController.php on line 43

任何想法?

但是,如果我打印它的结果,getUser()它会返回大量数据(大多数与 symfony 框架相关)。

4

1 回答 1

1

首先,您的实体中没有getNickname()方法,然后您应该使用.UsergetNick()

但我认为问题出在其他地方,您定义了一个oneToMany关系 betweetEventUser. 因此,一个事件可能与许多用户相关。然后,如果您调用getUser()给定事件,您将获得一组用户。然后,您应该迭代以获取可以调用的单个用户对象getNick()

于 2013-04-22T11:16:17.940 回答