12

I have entities in a OneToMany relation:

Forecast -> has many -> Brick(s)

I've created serialization mappings for each in Resources/config/serializer/Entity.xxx.yml where xxx is entity name.

Each entity has exclusion policy set to ALL and some of it's properties are exposed, eg:

Acme\ForecastBundle\Entity\Forecast:   
  exclusion_policy: ALL   
  xml_root_name: forecast   
  properties:
    id:
      expose: true
      type: integer
    regionUid:
      expose: true
      type: string
    description:
      expose: true
      type: string
    bricks:
      expose: true
      type: array<Acme\ForecastBundle\Entity\Brick>
      xml_list:
        inline: true
        entry_name: brick

When in my template I do {{ forecast|serialize|raw }} I am getting:

Forecast -> as I expected -> only exposed fields are serialized

Bricks collection -> all properties are serialized -> it seems that my Entity.Brick.yml is ignored... why?

In brick I have only ID and name exposed.. but in serialized output I have all properties (created_at, updated_at.. and more).. why? They should be excluded by "exclusion_policy: ALL". It seems that config for nested collection is not used.

Acme\ForecastBundle\Entity\Brick:   
  exclusion_policy: ALL   
  xml_root_name: brick
  properties:
    id:
      expose: true
      type: integer
    name:
      expose: true
      type: string

EDIT:

Yes, I did clear cache after each change in .yml config

After some suggestions I added @ExclusionPolicy("ALL") annotation to the Brick class and @Expose on ID, just to see what happens.. and suddenly it works! Not only ID is exposed, but everything is like in my YML configuration.

So I removed the annotations.. and it still works!

So it seems that adding Annotations somehow forced serializer to recognize my YML config. I don't know why though. That worries me.

Is it possible APC cache is guilty? I did clear Symfony2 cache numerous of times, but APC only a few.

4

2 回答 2

10

由于没有回答者,我将发布我最终所做/发现的内容(以供将来参考和遇到相同问题的任何人)。我将引用我的编辑:

经过一些建议后,我在 Brick 类中添加了 @ExclusionPolicy("ALL") 注释并在 ID 上添加了 @Expose,只是为了看看会发生什么......突然它起作用了!不仅暴露了 ID,而且一切都像我的 YML 配置一样。

所以我删除了注释..它仍然有效!

所以似乎添加注释以某种方式强制序列化程序识别我的 YML 配置。我不知道为什么。这让我很担心。

于 2014-01-22T10:35:40.230 回答
0

显然添加 use JMS\Serializer\Annotation\ExclusionPolicy; 到实体就够了。

我猜它不起作用,因为我们没有实现该类来这样做。

于 2017-01-04T16:40:10.357 回答