Reputation: 9454
I have an entity with a standard datetimetz field, with standard getter and setter:
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetimetz")
*/
private $date;
/**
* Get date
*
* @return \DateTime
*/
public function getDate() {
return $this->date;
}
/**
* Set date
*
* @param \DateTime $date
* @return ConsultationForm
*/
public function setDate($date) {
$this->date = $date;
return $this;
}
Serializing this works just fine, and the resulting JSON has a field with a string representing the date:
date: "2014-07-05T09:53:45+0200"
However, I would like to add a second method to my entity, which returns a Unix timestamp corresponding to my date object:
/**
* Get date as millis
*
* @return int
*/
public function getDateAsMillis() {
return $this->date->getTimestamp();
}
I would like the output of this method to also be encoded as a JSON field in the resulting object:
dateAsMillis: 3423435252345232
How can I instruct the FOSRestbundle or the serialiser to do this automatically?
Upvotes: 0
Views: 415
Reputation: 1822
You can use the VirtualPropery annotation (http://jmsyst.com/libs/serializer/master/reference/annotations#virtualproperty)
/**
* @JMS\VirtualProperty
* @JMS\SerializedName("dateAsMillis")
*/
public function getDateAsMillis() {
return $this->date->getTimestamp();
}
Upvotes: 1