Exception: Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken :: serialize () must return a string or NULL

I cannot authenticate to symfony2 using the Employee object, because it contains many mappings with other objects in my project. Some of my mappings are as follows:

/**
     * @var EmployeeDesignation
     *
     * @ORM\ManyToOne(targetEntity="EmployeeDesignation")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="employee_designation_id", referencedColumnName="id")
     * })
     */
    private $employeeDesignation;

    /**
     * @var EmployeeDesignation
     *
     * @ORM\ManyToOne(targetEntity="EmployeeType")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="employee_type_id", referencedColumnName="id")
     * })
     */
    private $employeeType;

Authentication works fine without any mapping. I tried using the Serialize () and 'Unserialize ()' methods as shown below:

class Employee implements AdvancedUserInterface, \Serializable{
     /**
             * serialize the username
             * @return serialize
             */
            public function serialize() {
              return serialize($this->emailOfficial);
            }

            /**
             * unserialize
             * @param $data
             */
            public function unserialize($data) {
              $this->em = unserialize($data);
            }

After executing the above method, I get the following error:

You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine. 

I tried this way to get rid of the previous error, which looks like this:

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL 

So, can anyone suggest a way to overcome this problem?

+5
source share
4

- , , .

- , , __sleep, .

class User implements PlayerInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
...

    public function __sleep()
    {
        return array('id');
    }
...
  • , , @ORM\Id, .
  • cookie , .

, ( ManyToMany), , :

// see Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse()
...
        $event->getRequest()
            ->getSession()
            ->set('_security_'.$this->contextKey, serialize($token));
...

, -.

Edit:

:

+19

:

class User implements UserInterface

class User implements UserInterface, \Serializable

User:

public function serialize() {
    return serialize($this->username);
}

public function unserialize($data) {
    $this->username = unserialize($data);
}
+2

There is another opportunity to solve this problem. You must make the visibility of all entity properties associated with your user “secure”

See: http://www.metod.si/symfony2-error-usernamepasswordtokenserialize-must-return-a-string-or-null/

+1
source

You must put "protected" in each variable of your custom class. At least for me this work :)

0
source

All Articles