Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>solution of this problem is very simple. All problems associated with circular references at yours <code>User</code> and <code>Role</code> objects. So you have not to serialize <code>User::$roles</code> and <code>Role::$users</code> fields.</p> <p>Look at <code>Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct()</code> and <code>Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize()</code>.</p> <p>How you can see, Symfony take your user's roles by invoking <code>UserInterface::getRoles()</code> before serialization. And serialize <code>User</code> and <code>Roles</code> separately.</p> <p>You have to implement <code>\Serializable</code> interface in <code>User</code> and <code>Role</code> entities.</p> <h2>Example:</h2> <pre class="lang-php prettyprint-override"><code>/** * 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-&gt;id, $this-&gt;username, $this-&gt;email, $this-&gt;salt, $this-&gt;password, $this-&gt;isActive )); } /** * @see \Serializable::unserialize() */ public function unserialize($serialized) { list ( $this-&gt;id, $this-&gt;username, $this-&gt;email, $this-&gt;salt, $this-&gt;password, $this-&gt;isActive ) = \unserialize($serialized); } } </code></pre> <pre class="lang-php prettyprint-override"><code>/** * 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-&gt;id, $this-&gt;role )); } /** * @see \Serializable::unserialize() */ public function unserialize($serialized) { list( $this-&gt;id, $this-&gt;role ) = \unserialize($serialized); } } </code></pre> <p>And all will be serialized/unserialized correctly.</p> <p>See discus at <a href="https://github.com/symfony/symfony/issues/3691">https://github.com/symfony/symfony/issues/3691</a></p> <p>See also: <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/entities-in-session.html#serializing-entity-into-the-session">http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/entities-in-session.html#serializing-entity-into-the-session</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload