@@ -54,7 +54,7 @@ focus on the most important methods that come from the
5454 * @ORM\Table(name="acme_users")
5555 * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
5656 */
57- class User implements UserInterface
57+ class User implements UserInterface, \Serializable
5858 {
5959 /**
6060 * @ORM\Column(type="integer")
@@ -132,6 +132,26 @@ focus on the most important methods that come from the
132132 public function eraseCredentials()
133133 {
134134 }
135+
136+ /**
137+ * @see \Serializable::serialize()
138+ */
139+ public function serialize()
140+ {
141+ return serialize(array(
142+ $this->id,
143+ ));
144+ }
145+
146+ /**
147+ * @see \Serializable::unserialize()
148+ */
149+ public function unserialize($serialized)
150+ {
151+ list (
152+ $this->id,
153+ ) = unserialize($serialized);
154+ }
135155 }
136156
137157 In order to use an instance of the ``AcmeUserBundle:User `` class in the Symfony
@@ -168,6 +188,15 @@ For more details on each of these, see :class:`Symfony\\Component\\Security\\Cor
168188 return $this->username === $user->getUsername();
169189 }
170190
191+ .. note ::
192+
193+ The :phpclass: `Serializable ` interface and its ``serialize `` and ``unserialize ``
194+ methods have been added to allow the ``User `` class to be serialized
195+ to the session. This may or may not be needed depending on your setup,
196+ but it's probably a good idea. Only the ``id `` needs to be serialized,
197+ because the :method: `Symfony\\ Bridge\\ Doctrine\\ Security\\ User\\ EntityUserProvider::refreshUser `
198+ method reloads the user on each request by using the ``id ``.
199+
171200Below is an export of my ``User `` table from MySQL. For details on how to
172201create user records and encode their password, see :ref: `book-security-encoding-user-password `.
173202
@@ -368,7 +397,7 @@ The code below shows the implementation of the
368397 throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
369398 }
370399
371- return $this->loadUserByUsername ($user->getUsername ());
400+ return $this->find ($user->getId ());
372401 }
373402
374403 public function supportsClass($class)
@@ -429,7 +458,7 @@ returns the list of related groups::
429458 use Doctrine\Common\Collections\ArrayCollection;
430459 // ...
431460
432- class User implements AdvancedUserInterface
461+ class User implements AdvancedUserInterface, \Serializable
433462 {
434463 /**
435464 * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
@@ -448,6 +477,26 @@ returns the list of related groups::
448477 {
449478 return $this->groups->toArray();
450479 }
480+
481+ /**
482+ * @see \Serializable::serialize()
483+ */
484+ public function serialize()
485+ {
486+ return serialize(array(
487+ $this->id,
488+ ));
489+ }
490+
491+ /**
492+ * @see \Serializable::unserialize()
493+ */
494+ public function unserialize($serialized)
495+ {
496+ list (
497+ $this->id,
498+ ) = unserialize($serialized);
499+ }
451500 }
452501
453502The ``AcmeUserBundle:Group `` entity class defines three table fields (``id ``,
0 commit comments