当实现2个接口时Symfony得到错误

时间:2018-07-13 10:34:26

标签: symfony

您好,我有一个Entity用户,我想像这样实现UserInterfaceObjectManagerAware

<?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 个答案:

答案 0 :(得分:2)

Symfony似乎尝试将整个User对象序列化到会话中,并且某些字段不可序列化。您应该实现\serializable接口,并仅将重要字段添加到serialize方法。

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));
}