我正在开发一个使用 symfony 2.5 构建的网站,要求是每个用户只需要拥有一个角色(用户不能拥有超过 1 个角色),因此在用户名和密码所在的用户表中还有另一列名字role
其中包含ROLE_ADMIN
对于管理员和ROLE_STAFF
对于公司员工,因此在验证我的getRoles
功能如下所示我可以以管理员身份正常登录
public function getRoles()
{
return array('ROLE_ADMIN');
}
因为我无法对角色进行硬编码,并且需要从数据库中获取它,所以如果我更改此设置以从role的列user table
public function getRoles()
{
return $this->getRole();
}
// getter for $role
public function getRole()
{
return $this->role->toArray();
}
我收到错误
错误:在非对象上调用成员函数 toArray()
如果有人可以帮助我解决这个问题,我将非常感激,请注意,我不需要角色的多对多关系,每个用户只需要一个角色,并且希望避免创建另一个实体,例如烹饪书示例。
如果有帮助的话这是我的全部User
Entity
<?php
namespace ClickTeck\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $salt;
/**
* @var boolean
*/
private $isActive;
/**
* @var string
*/
private $role;
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->role = new ArrayCollection();
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles()
{
//return $this->role;
return $this->getRole();
//return array('ROLE_ADMIN');
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->salt,
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* 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 isActive
*
* @param boolean $isActive
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set role
*
* @param string $role
* @return User
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role->toArray();
}
}
我还需要提到假设如果我不将角色转换为数组我会收到错误
可捕获的致命错误:传递给 Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() 的参数 4 必须是数组类型,给定字符串,