我需要一些幫助,SF 2.7串行Symfony的2.7日期時間序列化
我做了一個API使用get JSON數據是這樣的:
{
"dateDebut":"2017-02-16",
"dateFin":"2018-02-16",
"caMoisTotalHorsSessions":"5.2",
"caMoisClients":"5.3",
"caMoisGarantie":"5.4",
"caMoisHuile":"5.5" }
我試過很多方法,以deserialze到我的對象類其中dateDebut和dateFin出席是DateTime對象,而不是字符串
try {
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$callback = function ($date) {
return new \DateTime($date);
};
$normalizer->setCallbacks(array(
'dateDebut' => $callback,
'dateFin' => $callback,));
$serializer = new Serializer(array($normalizer), array($encoder));
$entity = $serializer->deserialize($request->getContent(), $class, $format);
} catch (RuntimeException $e) {
return new JsonResponse(
['code' => Response::HTTP_BAD_REQUEST, 'message' => $this->trans('api.message.data_error')],
Response::HTTP_BAD_REQUEST);
}
但從未使用過的回調:/任何人都可以幫我嗎?
目標是在數據庫中刷新對象之前自動將日期字符串轉換爲日期時間對象。
非常感謝
非常感謝,正是我會做:) –