symfony从json数据中持久化datetime

时间:2014-04-09 19:59:36

标签: php json symfony datetime

您好,我将数据作为JSON格式重现,在我的服务器网站上,我必须通过Doctrine存储它们。一切都很顺利,但当我收到日期时间格式数据时,我有验证错误。我在控制器中的分离操作上测试了这种情况:

public function indexAction($name)
{

    $em = $this->getDoctrine()->getManager();

    $test = new Test();
    $test->setName("Test");
    //$test->setStart(new \DateTime());
    $form = $this->createForm(new TestType(), $test);

    $store = array(
        "name" => "Test",
        "start" => new \DateTime()//will be something like *2014-04-09 11:11:11'
    );

    $form->submit($store);

    if ($form->isValid()) {
        $em->persist($test);
        $em->flush();
    } else var_dump($this->getErrorMessages($form));


    return $this->render('CodeTestBundle:Default:index.html.twig', array('name' => $name));
}

var dump是:

  

数组(大小= 1)'开始' =>       数组(大小= 3)         0 =>字符串'此值无效。' (长度= 24)

  'date' => 
    array (size=3)
      'year' => 
        array (size=0)
          ...
      'month' => 
        array (size=0)
          ...
      'day' => 
        array (size=0)
          ...
  'time' => 
    array (size=2)
      'hour' => 
        array (size=0)
          ...
      'minute' => 
        array (size=0)
          ...

3 个答案:

答案 0 :(得分:5)

您的问题是表单框架期望视图数据由每个日期和时间组件的一个小部件处理,因为datetime字段类型的默认小部件设置是choice。

如果将datetime字段配置为单个文本输入,则验证器将接收一个字符串而不是数组结构,并按预期处理它而不进行任何其他转换。在您的情况下,字段配置如下所示:

$builder->add('start', 'datetime', array(
    'widget' => 'single_text',
    'input' => 'datetime'
));

答案 1 :(得分:1)

嗯,你应该使用handleRequest:

public function indexAction($name, Request $request) // Add the Request
{

    $em = $this->getDoctrine()->getManager();

    $test = new Test();
    $test->setName("Test");
    //$test->setStart(new \DateTime());
    $form = $this->createForm(new TestType(), $test);

    $store = array(
        "name" => "Test",
        "start" => new \DateTime()//will be something like *2014-04-09 11:11:11'
    );

    // $form->submit($store);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->persist($test);
        $em->flush();
    } else var_dump($this->getErrorMessages($form));


    return $this->render('CodeTestBundle:Default:index.html.twig', array('name' => $name));
}

答案 2 :(得分:1)

你必须使用变压器:

namespace MyProject\SMyBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class StringToDateTimeTransformer implements DataTransformerInterface
{

public function reverseTransform($string)
{

    if (!$string) {
        return new \DateTime("now");
    }
    $date = new \DateTime((string)$string['date']);
    return $date;
}

public function transform($date)
{
    if (!$date) {
        return null;
    }

    $string = date_parse (  date_format($date, "Y-m-d H:i:s"));
    if (!$string) {
        throw new TransformationFailedException(sprintf(
            'Cant transform date to string!',
            $date
        ));
    }
    return $string;
}
}

并添加如下字段:

$sToDTTranformer  = new DatetimeToStringTransformer();
    $builder->add('datetime', 'text')
        ->addViewTransformer($sToDTTranformer);

更多信息:http://symfony.com/en/doc/current/cookbook/form/data_transformers.html