solution of this problem is very simple.
All problems associated with circular references at yours User
and Role
objects.
So you have not to serialize User::$roles
and Role::$users
fields.
Look at Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct()
and Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize()
.
How you can see, Symfony take your user's roles by invoking UserInterface::getRoles()
before serialization. And serialize User
and Roles
separately.
You have to implement \Serializable
interface in User
and Role
entities.
Example:
/**
* Acme\Bundle\UserBundle\Entity\User
*
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, EquatableInterface, \Serializable
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(type="string", length=30, unique=true)
*/
private $username;
/**
* @var string $email
*
* @ORM\Column(type="string", length=100, unique=true)
*/
private $email;
/**
* @var string $salt
*
* @ORM\Column(type="string", length=40)
*/
private $salt;
/**
* @var string $password
*
* @ORM\Column(type="string", length=128)
*/
private $password;
/**
* @var boolean $isActive
*
* @ORM\Column(type="boolean")
*/
private $isActive;
/**
* User's roles. (Owning Side)
*
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*/
private $roles;
// .....
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
/*
* ! Don't serialize $roles field !
*/
return \serialize(array(
$this->id,
$this->username,
$this->email,
$this->salt,
$this->password,
$this->isActive
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->email,
$this->salt,
$this->password,
$this->isActive
) = \unserialize($serialized);
}
}
/**
* Acme\Bundle\UserBundle\Entity\Role
*
* @ORM\Table(name="role")
* @ORM\Entity
*
*/
class Role implements RoleInterface, \Serializable
{
/**
* @var integer $id
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $role
*
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* Users in group (Inverse Side)
*
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
// .....
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
/*
* ! Don't serialize $users field !
*/
return \serialize(array(
$this->id,
$this->role
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->role
) = \unserialize($serialized);
}
}
And all will be serialized/unserialized correctly.
See discus at https://github.com/symfony/symfony/issues/3691
See also:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/entities-in-session.html#serializing-entity-into-the-session