0

First of all, sorry about my english.. I'll try to express in the better way.

I'm building an API Rest in Symfony 2.7 with JMSSerializerBundle. I got a Customer model with address properties separated: (line1, line2, city, postal_code, state, country_code), when i'm sending the response, i'm doing this:

{
"company_name": "Foograde",
"first_name": "Federico",
"last_name": "Balderas Mata",
"email": "federico.balderas@foograde.com.mx",
"address": {
  "line1": "Gral. Ortega #223D",
  "city": "Celaya",
  "state": "Guanajuato",
  "postal_code": "38010",
  "country_code": "MX"
}}

Like you see, i'm sending parameter on an address object:

/**
 * Get address
 * @VirtualProperty
 * @return array
 * @SerializedName("address")
 */
public function getAddress()
{
    return array(
        'line1' => $this->line1,
        'line2' =>  $this->line2,
        'line2' => $this->line3,
        'city' =>  $this->city,
        'state' => $this->state,
        'postal_code' => $this->postal_code,
        'country_code' => $this->country_code
    );
}

But now what i'm trying to do is get the request in the same form, with an address object and separate the properties to insert them in the database.

Any idea?

  • I don't really get what you want but at least I guess [Validating, serialising and mapping json request to model classes](http://www.inanzzz.com/index.php/post/nx2b/validating-serialising-and-mapping-json-request-to-model-classes) would come in handy for you. And maybe other mapping and modelling examples in that site. – BentCoder Oct 02 '15 at 21:13

2 Answers2

1

I think you should move address from Customer to its own entity, So, there would be Address entity and Customer entity with unidirectional manyToMany address field. By this way you can create a form for address, embed it to customer form, and you can also get validation ready for you..

xurshid29
  • 4,172
  • 1
  • 20
  • 25
1

as the docs from JMSSerializer state:

@VirtualProperty This annotation can be defined on a method to indicate that the data returned by the method should appear like a property of the object.

Note: This only works for serialization and is completely ignored during deserialization.

As thexurshid29 states, it is best to move the "Address" to a seperate entity, you can have the json output remain exactly the same as before, by using serializationgroups (to include/eclude the fields you want)

This way, you can also deserialize this exact same data, back into its appropriate entities, by simply using the JMSSerializer, to deserialize the json data.

Sam Janssens
  • 1,491
  • 1
  • 12
  • 30