我有一个安装了 JMSSerializerBundle 的 symfony 应用程序。
我的实体如下所示:
class MyEntity {
/**
* Attribute[]
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Attribute", mappedBy="myEntity")
* @JMS\Groups({"attributeSet_detail"})
* @ORM\OrderBy({"position" = "ASC"})
*/
protected $attributes;
}
AppBundle\Entity\Attribute
实体有一个 bool 属性 isActive
现在我想序列化 MyEntity
(包括所有属性
),但仅限属性 isActive
设置为 true
最佳答案
您可以使用 JMS Serializer 动态排除策略。
<?php
class MyObject
{
/**
* @Exclude(if="true")
*/
private $name;
/**
* @Expose(if="true")
*/
private $name2;
}
查看此处的文档:https://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies
关于symfony - JMSerializer 按属性条件排除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707253/