I have a simple problem but I have no idea how to solve it.
I have a project in Symfony2 with FOSRestBundle + JMSSerializerBundle. In my entities, I've got some datetime fields, and the user should be able to define the format of the datetime retrieved by the API, as you can see :
My problem is that I don't know how to tell JMSSerializerBundle to use the user-defined format for my datetime fields. Currently I have this :
/**
* @var \DateTime
*
* @ORM\Column(name="custom_date", type="datetime")
* @Serializer\Type("DateTime<Y-m-d>")
* @Serializer\Expose
*/
protected $custom_date;
The only way I found is to use the VirtualProperty method from JMSSerializerBundle, but it means that my entity must have a relation with the user. Something like that :
/**
* @Serializer\VirtualProperty
* @Serializer\Type("string")
* @Serializer\SerializedName("custom_date")
*/
public function getVPCustomDate() {
$format = $this->user->getSetting('date_format');
return $this->custom_date->format($format);
}
But it wouldn't make sense to do it that way, so I hope you have some alternatives!
Thanks for your time and your help.
