JMS Serializer DateTime not deserializing?

做~自己de王妃 提交于 2020-01-06 20:09:31

问题


I have a sort of special setup of Symfony2, FOSRestBundle and JMSSerializerBundle, due to a legacy DB in the background. I'm struggling to get a PUT request working, including the deserialization of a DateTime property.

First things first. This is a model snippet, notice the DateTime format:

/**
 * @var \DateTime
 *
 * @ORM\Column(name="sys_lastmodifieddate", type="datetime", nullable=true)
 *
 * @Type("DateTime<'Y-m-d\TH:i:sP'>")
 *
 * @Expose
 */
private $sysLastmodifieddate;

I have built a Form\Type to support persisting incoming entities:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('officialname')
        ->add('shortname')
        ->add('shortinfo')
        ->add('web')      
        ->add('fk_clientsector')      
        ->add($builder->create('sys_lastmodifieddate')->addViewTransformer(new DateTimeToStringTransformer()));
}

The controller action looks like this:

public function putClientAction(Request $request, $id )
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('AcmeBundle:Client')->find($id);

    $form = $this->get('form.factory')->createNamed('', new \AcmeBundle\ClientType(), $entity);
    $form->submit($request);

    $em->persist($entity);
    $em->flush();

    return $this->get('fos_rest.view_handler')->handle($this->view(null, Codes::HTTP_OK));

}

Now, my problem is that updates to any field coming in via JSON will get executed, but the DateTime field.

Example request, submitted via a PUT route:

{
    "shortname":"some client",
    "officialname":"some client",
    "sys_lastmodifieddate":"2016-06-13T11:20:49+01:00",
    "fk_clientsector":"5"
}

Resulting update statement from the log:

UPDATE crm_client SET shortname = ?, officialname = ?, fk_clientsector = ? WHERE id = ? ["some client", "some client", "5", 12345]

No sign of the date anywhere. Got any hints why this is happening, what I'm doing wrong? Will be happy to provide any requested info.

Note: I have already tried naming the Model property $sys_lastmodifieddate (lowercase and underscored), but that breaks the form altogether.

Thanks!

来源:https://stackoverflow.com/questions/37788608/jms-serializer-datetime-not-deserializing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!