تحصل Symfony على خطأ عند تنفيذ واجهتين

0

مرحبًا ، لدي مستخدم كيان ، أريد تنفيذه UserInterface و ObjectManagerAware مثله :

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\ObjectManagerAware;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserInterface;


/**
 * User
 *
 * @ORM\Table(name="User")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User implements UserInterface, ObjectManagerAware
{

    /**
     * @var string
     *
     * @ORM\Column(name="Id", type="string", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $Id;

    /**
     * @var string
     *
     * @ORM\Column(name="Username", type="string", length=255, nullable=false)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="Email", type="string", length=255, nullable=false)
     */
    private $Email;

    /**
     * @var string
     *
     * @ORM\Column(name="Password", type="string", length=255, nullable=false)
     */
    private $Password;

    /**
     * @var string
     *
     * @ORM\Column(name="Salt", type="string", length=5, nullable=false)
     */
    private $Salt;

    /**
     * @var string
     *
     * @ORM\Column(name="CreatedAt", type="string", length=255, nullable=true)
     */
    private $CreatedAt;

    /**
     * @var string
     *
     * @ORM\Column(name="ModifiedAt", type="string", length=255, nullable=true)
     */
    private $ModifiedAt;

    /**
     * @var string
     *
     * @ORM\Column(name="Fullname", type="string", length=255, nullable=true)
     */
    private $Fullname;

    /**
     * @var string
     *
     * @ORM\Column(name="Displayname", type="string", length=255, nullable=true)
     */
    private $Displayname;

    /**
     * @var string
     *
     * @ORM\Column(name="Firstname", type="string", length=255, nullable=true)
     */
    private $Firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="Middlename", type="string", length=255, nullable=true)
     */
    private $Middlename;

    /**
     * @var string
     *
     * @ORM\Column(name="Lastname", type="string", length=255, nullable=true)
     */
    private $Lastname;

    /**
     * @var string
     *
     * @ORM\Column(name="Birthplace", type="string", length=255, nullable=true)
     */
    private $Birthplace;

    /**
     * @var string
     *
     * @ORM\Column(name="Birthdate", type="string", length=255, nullable=true)
     */
    private $Birthdate;

    /**
     * @var string
     *
     * @ORM\Column(name="Gender", type="integer", length=255, nullable=true)
     */
    private $Gender;

    /**
     * @var string
     *
     * @ORM\Column(name="Job", type="string", length=255, nullable=true)
     */
    private $Job;

    /**
     * @var string
     *
     * @ORM\Column(name="IdNumber", type="string", length=255, nullable=true)
     */
    private $IdNumber;

    /**
     * @var string
     *
     * @ORM\Column(name="Notes", type="string", length=255, nullable=true)
     */
    private $Notes;

    /**
     * @var boolean
     *
     * @ORM\Column(name="IsActive", type="boolean", nullable=true)
     */
    private $IsActive;

    /**
     * @var string
     *
     * @ORM\Column(name="ContractNumber", type="string", length=255, nullable=true)
     */
    private $ContractNumber;

    /**
     * @var string
     */
    private $Role;

    /**
     * @var string
     */
    private $plainPassword;

    /**
     * @var string
     *
     * @ORM\Column(name="NikNumber", type="string", length=255, nullable=true)
     */
    private $NikNumber;


    public function __construct($username)
    {
        $now = new \DateTime("now");
        $nowText = $now->format('Y-m-d H:i:s');

        $this->setUsername($username);
        if ($this->getId() == null) {
            $this->setIsActive(true);
            $this->setCreatedAt($nowText);
            $this->setModifiedAt($nowText);
        } else {
            $this->setModifiedAt($nowText);
        }
    }

    public function getId()
    {
        return $this->Id;
    }

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

    public function getEmail()
    {
        return $this->Email;
    }

    public function getPassword()
    {
        return $this->Password;
    }

    public function getSalt()
    {
        return $this->Salt;
    }

    public function getCreatedAt()
    {
        return $this->CreatedAt;
    }

    public function getModifiedAt()
    {
        return $this->ModifiedAt;
    }

    public function getRoles()
    {
        return [
            'AWARDEE'
        ];
    }

    /*
  public function getUserRole(){
     $userRole = $this->em->getRepository('AppBundle:UserRole')->findOneBy(['UserId'=>$this->Id]);
     return $userRole->getRole();
  }
  */

    public function getRole()
    {
        return $this->Role;
    }

    public function setRole($role)
    {
        $this->Role = $role;
    }

    public function getFullname()
    {
        return $this->Fullname;
    }

    public function getDisplayname()
    {
        return $this->Displayname;
    }

    public function getFirstname()
    {
        return $this->Firstname;
    }

    public function getMiddlename()
    {
        return $this->Middlename;
    }

    public function getLastname()
    {
        return $this->Lastname;
    }

    public function getBirthdate()
    {
        return $this->Birthdate;
    }

    public function getBirthplace()
    {
        return $this->Birthplace;
    }

    public function getGender()
    {
        return $this->Gender;
    }

    public function getGenderName()
    {
        if ($this->Gender == 1) {
            return 'Laki-laki';
        } elseif ($this->Gender == 2) {
            return 'Perempuan';
        }

    }

    public function getJob()
    {
        return $this->Job;
    }

    public function getIdNumber()
    {
        return $this->IdNumber;
    }

    public function getIsActive()
    {
        return $this->IsActive;
    }

    public function getNotes()
    {
        return $this->Notes;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function getNikNumber()
    {
        return $this->NikNumber;
    }

    public function eraseCredentials()
    {

    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function setEmail($email)
    {
        $this->Email = $email;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }

    public function setPassword($password)
    {
        $this->Password = $password;
    }

    public function setSalt($item)
    {
        $this->Salt = $item;
    }

    public function setCreatedAt($item)
    {
        $this->CreatedAt = $item;
    }

    public function setModifiedAt($item)
    {
        $this->ModifiedAt = $item;
    }

    public function setFullname($item)
    {
        $this->Fullname = $item;
    }

    public function setDisplayname($item)
    {
        $this->Displayname = $item;
    }

    public function setFirstname($item)
    {
        $this->Firstname = $item;
    }

    public function setMiddlename($item)
    {
        $this->Middlename = $item;
    }

    public function setLastname($item)
    {
        $this->Lastname = $item;
    }

    public function setBirthdate($item)
    {
        $this->Birthdate = $item;
    }

    public function setBirthplace($item)
    {
        $this->Birthplace = $item;
    }

    public function setGender($item)
    {
        $this->Gender = $item;
    }

    public function setJob($item)
    {
        $this->Job = $item;
    }

    public function setIdNumber($item)
    {
        $this->IdNumber = $item;
    }

    public function setIsActive($item)
    {
        $this->IsActive = $item;
    }

    public function setNotes($item)
    {
        $this->Notes = $item;
    }

    public function setNikNumber($item)
    {
        $this->NikNumber = $item;
    }

    public function injectObjectManager(ObjectManager $objectManager, ClassMetadata $classMetadata)
    {
        $this->em = $objectManager;
    }
}

أحتاج إلى ObjectManagerAware لإضافة وظيفة استعلام مخصصة في هذا الكيان.

لكني حصلت على خطأ:

HTTP 500 Internal Server Error
Serialization of 'Closure' is not allowed

لقد حصلت على خطأ فقط عند الوصول إلى المستخدم الذي قام بتسجيل الدخول ، ولكن إذا قمت بتسجيل الخروج ، فلن يكون الويب خطأ

لماذا حصلت على خطأ ، وكيفية حلها؟

1 إجابة

2
افضل جواب

يبدو أن Symfony يحاول إجراء تسلسل لكائن مستخدم بالكامل في الجلسة وبعض الحقول غير قابلة للتسلسل. يجب عليك تنفيذ \serializable الواجهة وإضافة الحقول المهمة فقط إلى طريقة التسلسل.

class User implements UserInterface, ObjectManagerAware, \Serializable 

{
  ...
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password
    ));
}


public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password
    ) = unserialize($serialized, array('allowed_classes' => false));
}
:مؤلف

أسئلة ذات صلة

فوق
قائمة طعام