symfony - Symfony 4 - 不明白如何使用 JMS 序列化器序列化实体
问题描述
我发现自己陷入了我的项目 Symfony 4。事实上,我有一个实现Serializable的用户实体和两个serialize
和unserialize
(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 选项。
但我不明白怎么做
解决方案
使用JMSSerializerBundle的方法很少:
直接来自您的实体,使用这样的
@Annotations
ans@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; }
从控制器代码中,在一个动作上:
$serializer = $container->get('jms_serializer');
$serializer->serialize($user, 'json');
$data = $serializer->deserialize($jsonData, 'YourNamespace\User', 'json');
- 使用配置文件:请参阅配置参考
希望这可以帮助,
推荐阅读
- javascript - 如何在 ReactNative 中检查嵌套变量的可用性而不检查所有前面的变量可用性?
- html - 获取列表类并通过 Unicode 将其更改为任何语言
- plpgsql - 没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。插入准备
- c# - 在给定力时计算 FixedUpdate 中的未来速度,并计算它是否对当前速度为负
- java - 我想写代码来更新Java中的mysql数据。这是我的代码。它给出了这样的错误:
- c# - 如何在 C# 中选择两列合并 lambda 表达式?
- excel - VBA Excel:错误代码 438 对象在我的 VBA 代码中不支持此属性
- angularjs - 为什么使用 ng-include 时 pushState 会中断?
- prometheus - AWS cloudwatch 指标未显示在 Prometheus 查询浏览器中,但是 cloudwatch_exporter 端点显示所有指标
- javascript - 获取字典的样本/切片,前 X 个键