JMS序列化器使用不同视图序列化对象中的对象 [英] JMS Serializer serialize object in object with diffrent view

查看:114
本文介绍了JMS序列化器使用不同视图序列化对象中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2开发RESTful服务, JMS序列化程序包 Hateoas包.我有两个实体,用户和公司,当我对公司进行序列化时,我想要得到更大的细节. 但是,在序列化与用户相关的公司时,仅显示公司ID和名称对象,或者仅显示ID为整数.

I'm developing a RESTful service with Symfony2, JMS Serializer Bundle, FOS Rest Bundle and Hateoas Bundle. There are 2 entities User and Company and I want to, when I serialize a Company get larger detail. But, when serializing User related Company show only Company ID and name object or just ID as integer.

我有如下的序列化政策.

I have serialize policy like below.

用户

Acme\UserBundle\Entity\User:
exclusion_policy: ALL
xml_root_name: user
properties:
    id:
        expose: true
        type: integer
    company:
        expose: true
        type: Acme\CompanyBundle\Entity\Company
    name:
        expose: true
        type: string
    surname:
        expose: true
        type: string
    picture:
        expose: true
        type: string
relations:
    -
        rel: self
        href:
            route: acme_v1_get_user
            parameters:
                id: expr(object.getId())
            absolute: true

公司

Acme\CompanyBundle\Entity\Company:
exclusion_policy: ALL
xml_root_name: company
properties:
    id:
        expose: true
        type: integer
    name:
        expose: true
        type: string
    address:
        expose: true
        type: string
    phone:
        expose: true
        type: string
    web:
        expose: true
        type: string
    created_date:
        expose: true
        type: DateTime
    updated_date:
        expose: true
        type: DateTime
    status:
        expose: true
        type: integer
relations:
    -
        rel: self
        href:
            route: acme_v1_get_company
            parameters:
                id: expr(object.getId())
            absolute: true

预期输出

{
  "id": 1,
  "name": "Jenny",
  "surname": "Doe",
  "picture": "http://google.com/kittens.jpg",
  "info": [],
  "company": {
    "id": 1,
    "name": "Demo Company"
  }
}

OR

{
  "id": 1,
  "name": "Jenny",
  "surname": "Doe",
  "picture": "http://google.com/kittens.jpg",
  "info": [],
  "company": 1
}

我得到的

{
  "id": 1,
  "name": "Jenny",
  "surname": "Doe",
  "picture": "http://google.com/kittens.jpg",
  "info": [],
  "company": {
    "id": 1,
    "name": "Demo Company",
    "address": "Lorem ipsum dolor sit amet",
    "phone": "0902124440444",
    "web": "http://www.demo-company.com",
    "created_date": "2015-07-22T11:21:03+0300",
    "updated_date": "2015-07-24T01:50:39+0300",
    "status": 1
  }
}

推荐答案

您可以使用组

AppBundle\Entity\User\User:
    exclusion_policy: ALL
    properties:
        lastname:
            expose: true
            groups: [info]

并带有注释,您可以定义哪个属性显示在哪个组上.最后,您可以为您使用的每条路线分配一个组.

And with an annotation, you can define which property is displayed on which group. And finally, you can assign a group to every route you use.

或者您可以像这样使用虚拟属性:

Or you can use virtual properties like so :

AppBundle\Entity\User\User:
    exclusion_policy: ALL
    properties:
         […]
    virtual_properties:
        getCompanyId:
            serialized_name: company
            type: string
            groups: [info]

然后您在用户实体中创建一个getCompanyId()方法,该方法返回companyId

And you create a getCompanyId() method in your User entity, that returns the companyId

这篇关于JMS序列化器使用不同视图序列化对象中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆