symfony从json数据中持久化日期时间



你好,我以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转储为:

array(size=1)'start'=>数组(大小=3)0=>string'此值无效。'(长度=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)
          ...

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

如果将日期时间字段配置为单个文本输入,则验证器将接收一个字符串而不是数组结构,并按预期进行处理,而无需进行任何额外的转换。在您的情况下,字段配置看起来像:

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

Hum,您应该使用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));
}

您必须使用转换器:

namespace MyProjectSMyBundleFormDataTransformer;
use SymfonyComponentFormDataTransformerInterface;
use SymfonyComponentFormExceptionTransformationFailedException;
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

相关内容

  • 没有找到相关文章