JMSerializer 按属性条件排除实体



我有一个安装了JMSSerializerBundle的symfony应用程序。
我的实体如下所示:

class MyEntity {
/**
* Attribute[]
*
* @ORMOneToMany(targetEntity="AppBundleEntityAttribute", mappedBy="myEntity")
* @JMSGroups({"attributeSet_detail"})
* @ORMOrderBy({"position" = "ASC"})
*/
protected $attributes;
}

AppBundleEntityAttribute实体具有布尔属性isActive

现在我想序列化MyEntity(包括所有attributes(,但只序列化那些属性isActive设置为true

您可以使用 JMS 序列化程序动态排除策略。

<?php
class MyObject
{
/**
* @Exclude(if="true")
*/
private $name;
/**
* @Expose(if="true")
*/
private $name2;
}

查看此处的文档:https://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies

最新更新