【问题标题】:Doctrine: Undefined index when trying to access one-to-many php教义:尝试访问一对多 php 时未定义的索引
【发布时间】:2019-03-25 03:31:01
【问题描述】:

我对教义有疑问。我有两个实体,想要实现一对多关联。

用户

/**
 * @Entity
 */
class User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id;

    /** @Column(type="string") */
    protected $name;

    /**
     * @OneToMany(targetEntity="role", mappedBy="userId", cascade={"ALL"})
     */
    private $roles;
}

角色

/**
 * @Entity
 */
class Role
{
    /** @Id @Column(type="integer") 
     *
     * @ManyToOne(targetEntity="user", inversedBy="roles") 
     * 
     */
     protected $userId;

     /** @Column(type="string") */
     protected $name;
 }

但是当我尝试访问用户的角色时:

var_dump($user->getRoles()->first()->getName());

注意:未定义索引:userId 在 /home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php 1794 行警告:为
中的 foreach() 提供的参数无效 /home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php 在第 1798 行 string(10) "ROLE_ADMIN" 警告:在 /home 中为 foreach() 提供的参数无效/ubuntu/workspace/atlas/Controller/LoginController.php 第 15 行"

这意味着我得到了正确的值,但有一些警告,我不确定是否可以忽略它们。

【问题讨论】:

    标签: php doctrine-orm one-to-many


    【解决方案1】:

    一个用户可以拥有多个角色
    一个角色可以属于多个用户

    所以,关联是ManyToMany

    您的映射定义错误。请考虑用这个替换它:

    用户

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    
    /**
     * @Entity
     */
    class User
    {
        /** 
         * @Id 
         * @Column(type="integer")
         * @GeneratedValue
         */
        protected $id;
    
        /**
         * @Column(type="string")
         */
        protected $name;
    
        /**
         * @ManyToMany(targetEntity="Role", inversedBy="users")
         */
        protected $roles;
    
        public function __construct()
        {
            $this->roles = new ArrayCollection();
        }
    
        public function getRoles()
        {
            return $this->roles;
        }
    
        public function addRole($role)
        {
            if (this->getRoles()->contains($role)) {
                return;
            } else {
                $this->getRoles()->add($role);
                $role->addUser($this);
            }
        }
    
        public function removeRole($role)
        {
            if (!this->getRoles()->contains($role)) {
                return;
            } else {
                $this->getRoles()->removeElement($role);
                $role->removeUser($this);
            }
        }
    }
    

    角色

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    
    /**
     * @Entity
     */
    class Role
    {
        /**
         * @Id
         * @Column(type="integer") 
         * @GeneratedValue
         */
        protected $id;
    
        /**
         * @Column(type="string")
         */
        protected $name;
    
        /**
         * @ManyToMany(targetEntity="User", mappedBy="roles") 
         */
        protected $users;
    
        public function __construct()
        {
            $this->users = new ArrayCollection();
        }
    
        public function getUsers()
        {
            return $this->users;
        }
    
        public function addUser($user)
        {
            if (this->getUsers()->contains($user)) {
                return;
            } else {
                $this->getUsers()->add($user);
                $user->addRole($this);
            }
        }
    
        public function removeUser($user)
        {
            if (!this->getUsers()->contains($user)) {
                return;
            } else {
                $this->getUsers()->removeElement($user);
                $user->removeRole($this);
            }
        }
     }
    

    记住:

    1. PHP 是区分大小写的,因此请仔细阅读文档。
    2. 类名应以大写字母开头。

    【讨论】:

    • 非常感谢!就是这样。我唯一的问题是,当我调用 user->getRoles() 而不是 ArrayCollection 时,我得到了一个持久性集合。你知道为什么会这样吗?
    • 没关系。 Doctrine 使用 PersistentCollection 来延迟加载实体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多