【问题标题】:Role entities in DoctrineDoctrine 中的角色实体
【发布时间】:2015-10-01 14:05:14
【问题描述】:

我正在尝试使用 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)

但刷新页面后失败。所以这里发生了奇怪的不一致

【问题讨论】:

    标签: php symfony doctrine-orm symfony-2.7


    【解决方案1】:

    经过一些研究和测试,这个问题似乎与 PHP 5.4 的问题有关,即使我使用的是 PHP 5.6 (RoleInterface throws "call on a non-object" error)

    我尝试使用 JSON 函数更改序列化过程,问题现在消失了。所以现在serializeunserialize 正在制造一个问题,但我不知道为什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多