我在symfony2中开发auth,我的用户实体与用户类别实体连接在一起。当我尝试登录时,Symfony会引发错误:
注意:serialize():;; quepatecoryId; quary'返回__sleep()的成员变量,但在供应商/symfony/symfony/symfony/src/symfony/component/component/security/core security/core/authetication/tocken/abfacttoken.php line中不存在149
用户实体:
/**
* Class User
* @package UsersBundleEntity
*/
class User implements UserInterface
{
/**
* @var integer $intuserid
*/
private $intuserid;
/**
* @var string $varpassword
*/
private $varpassword;
/**
* @var string $varsalt
*/
private $varsalt;
/**
* @var mixed $category
*/
private $category;
/**
* @return integer $intUserID.
*/
public function getIntUserID()
{
return $this->intuserid;
}
// ......... //
/**
* @return UserCategory $category
*/
public function getCategory()
{
return $this->category;
}
/**
* @param int $value
*
* @return $this
*/
public function setCategory($value = null)
{
$this->category = $value;
return $this;
}
/**
* Конструктор класса User
*/
public function __construct()
{
$this->userRoles = new ArrayCollection();
$this->category = new UserCategory();
$this->timeregister = new DateTime();
}
}
用户类别实体:
/**
* UserCategory
*/
class UserCategory
{
/**
* @var integer $intusercategoryid
*/
private $intusercategoryid;
// ...... //
/**
* Get intusercategoryid
*
* @return integer
*/
public function getIntUserCategoryId()
{
return $this->intusercategoryid;
}
}
问题描述
几天后,我发现了这个问题。我的实体变量访问中的问题(有问题是 private )。
我阅读了有关序列化的PHP文档,并发现:
对象的私人成员的班级名称已添加到成员名称;受保护的成员对成员名称有一个"*"。这些预审算的值的两侧为无效字节。
重要
在Symfony 2官方文档中,实体字段使用私有访问
生成文档中的示例:
// src/Acme/BlogBundle/Entity/BlogComment.php
namespace AcmeBlogBundleEntity;
use DoctrineORMMapping as ORM;
/**
* AcmeBlogBundleEntityBlogComment
*
* @ORMTable(name="blog_comment")
* @ORMEntity
*/
class BlogComment
{
/**
* @var integer $id
*
* @ORMColumn(name="id", type="bigint")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $author
*
* @ORMColumn(name="author", type="string", length=100, nullable=false)
*/
private $author;
/**
* @var text $content
*
* @ORMColumn(name="content", type="text", nullable=false)
*/
private $content;
/**
* @var datetime $createdAt
*
* @ORMColumn(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var BlogPost
*
* @ORMManyToOne(targetEntity="BlogPost")
* @ORMJoinColumn(name="post_id", referencedColumnName="id")
*/
private $post;
}
解决方案
为了解决错误,我更改了私有访问受保护的访问权限:
用户实体:
/**
* Class User
* @package UsersBundleEntity
*/
class User implements UserInterface
{
/**
* @var integer $intuserid
*/
protected $intuserid;
/**
* @var string $varpassword
*/
protected $varpassword;
/**
* @var string $varsalt
*/
protected $varsalt;
/**
* @var mixed $category
*/
protected $category;
/**
* @return integer $intUserID.
*/
public function getIntUserID()
{
return $this->intuserid;
}
// ......... //
/**
* @return UserCategory $category
*/
public function getCategory()
{
return $this->category;
}
/**
* @param int $value
*
* @return $this
*/
public function setCategory($value = null)
{
$this->category = $value;
return $this;
}
/**
* Конструктор класса User
*/
public function __construct()
{
$this->userRoles = new ArrayCollection();
$this->category = new UserCategory();
$this->timeregister = new DateTime();
}
}
用户类别实体:
/**
* UserCategory
*/
class UserCategory
{
/**
* @var integer $intusercategoryid
*/
protected $intusercategoryid;
// ...... //
/**
* Get intusercategoryid
*
* @return integer
*/
public function getIntUserCategoryId()
{
return $this->intusercategoryid;
}
}