首页 > 解决方案 > Symfony 4 - 不明白如何使用 JMS 序列化器序列化实体

问题描述

我发现自己陷入了我的项目 Symfony 4。事实上,我有一个实现Serializable的用户实体和两个serializeunserialize(PHP)方法来序列化我的实体。

除了在某一时刻我有一个循环引用问题。我看到我可以使用“深度选项”来纠正这个问题。JMS 序列化器很容易处理它。

所以我安装了JMS Serializer bundle

但是在浏览了几个站点,几个 doc' 之后,我不明白我如何修改我的 User 实体以使用 JMS 的序列化而不是 PHP 的序列化,并使用深度。

class User implements UserInterface, Serializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * Adresse email de l'utilisateur
     * @ORM\Column(type="string", length=180, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email(message="Veuillez renseigner un email valide")
     */
    private $email;

    /**
     * Rôles de l'utilisateur
     * @ORM\Column(type="json")
     */
    private $roles = [];


    /**
     * Ordres de mission de l'utilisateur
     * @ORM\OneToMany(targetEntity="App\Entity\OrdreMission", mappedBy="user")
     * @MaxDepth(2)
     */
    private $ordreMissions;


    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Entreprise", inversedBy="users")
     * @ORM\JoinColumn(nullable=false)
     * @MaxDepth(2)
     */
    private $entreprise;

/**
 * String representation of object
 * @link http://php.net/manual/en/serializable.serialize.php
 * @return string the string representation of the object or null
 */
public function serialize()
{
    return serialize([
        $this->id,
        $this->email,
        $this->password,
    ]);
}

/**
 * Constructs the object
 * @link http://php.net/manual/en/serializable.unserialize.php
 * @param string $serialized <p>
 * The string representation of the object.
 * </p>
 * @return void
 */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->email,
        $this->password,
        ) = unserialize($serialized);
}

我在我的“ ordreMission ”和“ entreprises ”属性上添加了@MaxDepth(2) ,我想使用 JMS Serializer 来序列化我的实体,并使用 depth 选项

但我不明白怎么做

标签: symfonyjmscircular-dependencydepthserialization

解决方案


使用JMSSerializerBundle的方法很少:

  1. 直接来自您的实体,使用这样的@Annotationsans @MaxDepth

    namespace YourNamespace\Entity;
    use Doctrine\ORM\Mapping as ORM;
    use JMS\Serializer\Annotation as Serializer;
    use JMS\Serializer\Annotation\MaxDepth as MaxDepth;
    
    /**
     * User
     *
     * @ORM\Table(name="user")
     * @ORM\Entity(repositoryClass="YourBundle\Repository\ProgrammerRepository")
     * @Serializer\ExclusionPolicy("all")
     */ 
    class User implements UserInterface
    {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       * @Serializer\Expose
       */
      private $id;
    
      /*[...]*/
    
      /**
       * Ordres de mission de l'utilisateur
       * @ORM\OneToMany(targetEntity="App\Entity\OrdreMission", mappedBy="user")
       * @MaxDepth(2)
       */
      private $ordreMissions;
    
    
      /**
       * @ORM\ManyToOne(targetEntity="App\Entity\Entreprise", inversedBy="users")
       * @ORM\JoinColumn(nullable=false)
       * @MaxDepth(2)
       */
      private $entreprise;
    }
    
  2. 从控制器代码中,在一个动作上:

    $serializer = $container->get('jms_serializer');
    $serializer->serialize($user, 'json');
    $data = $serializer->deserialize($jsonData, 'YourNamespace\User', 'json'); 
  1. 使用配置文件:请参阅配置参考

希望这可以帮助,


推荐阅读