Doctrine:尝试访问一对多时未定义的索引 php

Doctrine: Undefined index when trying to access one-to-many php

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

用户

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

Notice: Undefined index: userId in /home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php on line 1794 Warning: Invalid argument supplied for foreach() in
/home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php on line 1798 string(10) "ROLE_ADMIN" Warning: Invalid argument supplied for foreach() in /home/ubuntu/workspace/atlas/Controller/LoginController.php on line 15 "

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

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

所以,关联是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 是 CaSe sEnsiTiVE,所以请仔细阅读文档。
  2. class 名称应以大写字母开头。