我正在尝试使用 Doctrine 来保持我的用户的角色。
实体已启动并正在运行,但是当我尝试登录时出现此错误:
RoleHierarchy.php 第 43 行中的 FatalErrorException:错误:调用字符串上的成员函数 getRole()
我试图看看这个“字符串”是什么,它是:
array (size=1)
0 => string 'Europe/London' (length=13)
这是我的角色实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Role
* @package AppBundle\Entity
*
* @ORM\Entity()
* @ORM\Table("fxm_role")
* @ORM\HasLifecycleCallbacks()
*/
class Role implements RoleInterface
{
/**
* @var integer $id
*
* @ORM\Column( type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $role
*
* @ORM\Column(type="string")
*/
private $role;
/**
* @var string $name
*
* @ORM\Column(type="string")
*/
private $name;
/**
* @var \DateTime $created
*
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @var \DateTime $updated
*
* @ORM\Column(type="datetime")
*/
protected $updated;
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdated(new \DateTime('now'));
if ($this->getCreated() == null) {
$this->setCreated(new \DateTime('now'));
}
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
对于用户,这里是角色的使用部分
/**
* @var ArrayCollection $roles
*
* @ORM\ManyToMany(targetEntity="Role", cascade={"persist"})
* @ORM\JoinTable(name="users_roles",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return Role[] The user roles
*/
public function getRoles()
{
return $this->roles->toArray();
}
我真的不知道是什么问题,但是因为我什至没有分析器栏,所以由于某种原因甚至没有加载实体。
角色层次结构也不复杂:
role_hierarchy:
ROLE_BACKOFFICE: ROLE_USER
ROLE_ADMIN: ROLE_BACKOFFICE
如果有人知道如何解决这个问题,那就太好了,我已经坚持了一段时间了(而且作为一个实体的角色并没有太多的文档)。
更新:出于某种奇怪的原因,它曾经给我角色数组
array (size=1)
0 =>
object(AppBundle\Entity\Role)[437]
private 'id' => int 1
private 'role' => string 'ROLE_BACKOFFICE' (length=15)
private 'name' => string 'Back Office' (length=11)
protected 'created' =>
object(DateTime)[370]
public 'date' => string '2015-07-13 11:43:31.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
protected 'updated' =>
object(DateTime)[365]
public 'date' => string '2015-07-13 11:43:31.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
但是在我刷新页面后失败了。所以这里发生了一个奇怪的不一致