I am not able to authenticate in symfony2 with the 'Employee' entity as it contains many mapping with other entities in my project. some of my mapping is 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 have tried with 'Serialize()' and 'Unserialize()' methods in it like 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);
}
}
I am getting the following error after doing the above method:
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 have tried this way so as to get rid of the previous error, which is as follows:
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL
So, can anybody please suggest a way to overcome from this problem?
I have encountered something similar, and after some research, I tried the same things as you did.
But at some point, I found out that by setting the __sleep
method, every thing worked fine.
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
is part of the returned array.I don't know exactly why it causes this when setting up a new association (mine was a ManyToMany), but It probably originate from this place:
// see Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse() ... $event->getRequest() ->getSession() ->set('_security_'.$this->contextKey, serialize($token)); ...
Hope this could help someone.
Edit:
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With