I\'m trying to serialize a entity relation with JMS Serializer.
Here is the Entity:
class Ad
{
/**
* @Type(\"string\")
* @Groups({\"manag
The author wants to keep the property name, which doesn't apply to the accepted answer. As far as I understood, the answer by ScayTrase would keep the original property name but has another disadvantage according to the comments: The related object will be fetched if you are using Doctrine ORM @ManyToOne
, thus decreasing performance.
If you want to keep the original property name, you have to define the @VirtualProperty
at class level and @Exclude
the original property. Otherwise, the serialized property name will be derived from the getter method (countryId
in this case):
/**
* @Serializer\VirtualProperty(
* "country",
* exp="object.getCountryId()",
* options={@Serializer\SerializedName("country")}
* )
*/
class Ad {
/**
* @Serializer\Exclude
*/
private $country;
public function getCountryId() {
return $this->country === null ? null : $this->country->getId();
}
}