Skip to content

Commit fac4580

Browse files
alex88weaverryan
authored andcommitted
Updated "Load security users from database"
I've updated the "How to load Security Users from the Database" cookbook to include fix this error: PHP Fatal error:  Call to a member function getRole() on a non-object in /vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php on line 47 which is happening in PHP > 5.4. The fix is taken by this issue: symfony/symfony#3691 (comment)
1 parent e6aae29 commit fac4580

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

cookbook/security/entity_provider.rst

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ returns the list of related groups::
422422
use Doctrine\Common\Collections\ArrayCollection;
423423
// ...
424424

425-
class User implements AdvancedUserInterface
425+
class User implements AdvancedUserInterface, \Serializable
426426
{
427427
/**
428428
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
@@ -441,6 +441,36 @@ returns the list of related groups::
441441
{
442442
return $this->groups->toArray();
443443
}
444+
445+
/**
446+
* @see \Serializable::serialize()
447+
*/
448+
public function serialize()
449+
{
450+
return \serialize(array(
451+
$this->id,
452+
$this->username,
453+
$this->email,
454+
$this->salt,
455+
$this->password,
456+
$this->isActive
457+
));
458+
}
459+
460+
/**
461+
* @see \Serializable::unserialize()
462+
*/
463+
public function unserialize($serialized)
464+
{
465+
list (
466+
$this->id,
467+
$this->username,
468+
$this->email,
469+
$this->salt,
470+
$this->password,
471+
$this->isActive
472+
) = \unserialize($serialized);
473+
}
444474
}
445475

446476
The ``AcmeUserBundle:Group`` entity class defines three table fields (``id``,
@@ -461,7 +491,7 @@ that forces it to have a ``getRole()`` method::
461491
* @ORM\Table(name="acme_groups")
462492
* @ORM\Entity()
463493
*/
464-
class Group implements RoleInterface
494+
class Group implements RoleInterface, \Serializable
465495
{
466496
/**
467497
* @ORM\Column(name="id", type="integer")
@@ -499,6 +529,30 @@ that forces it to have a ``getRole()`` method::
499529
{
500530
return $this->role;
501531
}
532+
533+
/**
534+
* @see \Serializable::serialize()
535+
*/
536+
public function serialize()
537+
{
538+
return \serialize(array(
539+
$this->id,
540+
$this->name,
541+
$this->role
542+
));
543+
}
544+
545+
/**
546+
* @see \Serializable::unserialize()
547+
*/
548+
public function unserialize($serialized)
549+
{
550+
list(
551+
$this->id,
552+
$this->name,
553+
$this->role
554+
) = \unserialize($serialized);
555+
}
502556
}
503557

504558
To improve performances and avoid lazy loading of groups when retrieving a user

0 commit comments

Comments
 (0)