Symfony2-JMS序列化器-如果getDeleted()不为null,则排除实体 [英] Symfony2 - JMS Serializer - Exclude entity if getDeleted() is not null
问题描述
我已经嵌套了OneToMany关系.表单的问题部分包含问题组,这些问题组包含的问题可能包含答案.
I have nested OneToMany relationships. A form has question sections which have question groups that have questions which have possible answers.
我需要实现的是,忽略那些被删除(实际上使用$ deleted参数)的部分或组或问题或可能的答案
What I need to achieve is, to ignore those sections or groups or questions or possible answers that are deleted (virtually using $deleted param)
不排除属性或仅排除属性(带有表达式)的序列化效果很好.
Serializing without exclusion or by just excluding property (with expression) works fine.
但是类(非属性)上的@JMS \ Exclude(if ="expression ..")被忽略.
But @JMS\Exclude(if="expression..") on class (not property) is ignored.
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity
* @ORM\Table(name="entity_question_section")
* @JMS\Exclude(if="object.getDeleted() !== null")
*/
class QuestionSection
{
.......
}
允许在类"@Target({" PROPERTY," CLASS," METHOD," ANNOTATION})"上使用Exclude注释,但是它不起作用.
The Exclude annotation is allowed to use on class "@Target({"PROPERTY", "CLASS", "METHOD", "ANNOTATION"})" but it does not work.
无论使用哪种表达式,它都会排除QuestionSection实体内的所有字段,并为每个QuestionSection返回空对象.
Does not matter what kind of expression i use, it will exclude all fields inside QuestionSection entity and returns empty object for each QuestionSection.
我如何序列化
$serializer = $this->get('jms_serializer');
$serialized = $serializer->serialize($form, "json");
$response = new JsonResponse();
$response->setContent($serialized);
如果我需要通过嵌套循环来构建json响应来做到这一点,那可能是非常糟糕的解决方案.
If I will need to do it by doing nested loops to build json response it will be probably very bad solution.
我已经花了几个小时来完成这项工作,我也尝试过JLM \ SerializerExpressionBundle \ JLMSerializerExpressionBundle(),但它仅支持在属性上定义,并且仅排除属性.
I have googled few hours to get this done, i have also tried JLM\SerializerExpressionBundle\JLMSerializerExpressionBundle() but it only supports to be defined on property and only exclude property.
我也尝试过使用service(if ="service('general').isDeleted(object)")进行表达
I have also tried expression using service (if="service('general').isDeleted(object)")
谁能给我正确的方向来解决这个问题?
Can anyone give me the right direction to solve this?
谢谢
推荐答案
感谢@Rufinus,我使用了 http://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html 解决了我的问题.现在,已删除"实体将从任何查询中排除,因此序列化程序仅返回所需的数据.
Thanks to @Rufinus i have used http://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html which solved my problem. Now "deleted" entities are exluded from any query so serializer returns required data only.
这篇关于Symfony2-JMS序列化器-如果getDeleted()不为null,则排除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!