symfony - JMS Serializer serialize object in object with diffrent view -
i'm developing restful service symfony2, jms serializer bundle, fos rest bundle , hateoas bundle. there 2 entities user , company , want to, when serialize company larger detail. but, when serializing user related company show company id , name object or id integer.
i have serialize policy below.
user
acme\userbundle\entity\user: exclusion_policy: 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
company
acme\companybundle\entity\company: exclusion_policy: 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
expected output
{ "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 }
what got
{ "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 } }
you can use groups
appbundle\entity\user\user: exclusion_policy: properties: lastname: expose: true groups: [info]
and annotation, can define property displayed on group. , finally, can assign group every route use.
or can use virtual properties :
appbundle\entity\user\user: exclusion_policy: properties: […] virtual_properties: getcompanyid: serialized_name: company type: string groups: [info]
and create getcompanyid()
method in user entity, returns companyid
Comments
Post a Comment