【问题标题】:symfony doctrine type datetime expected format y-m-d h:i:ssymfony 学说类型日期时间预期格式 y-m-d h:i:s
【发布时间】:2014-03-12 08:58:34
【问题描述】:

当我尝试从具有原则的数据库中选择一些项目时,我在 symfony 中遇到问题。

我运行这个:

$allOrders = $em->getRepository('MyBundle:Dieta')->findBy(array('diet_status'=>'active'));
var_dump($allOrders);

我收到以下错误:

无法将数据库值“2014-03-03”转换为 Doctrine 类型 约会时间。预期格式:Y-m-d H:i:s

当我在数据库中插入日期时,我会这样做:

$format = 'd/m/Y';
$date = \DateTime::createFromFormat($format, $_POST['dietStartDate']);
$dietStartDate = $date->format('Y-m-d H:i:s');
$dietStartDate = new \Datetime($dietStartDate);

$timestamp = strtotime(str_replace('/', '-', $_POST['dietStartDate']));
$timestamp7 = strtotime('+7 days', $timestamp);
$datetime = date("Y-m-d H:i:s", $timestamp7);
$dietDateEnd = new \Datetime($datetime);

$dietStartDate 和 $dietDateEnd 被插入到数据库的日期时间列中。在教义.yml 中,我也将它声明为日期时间。

有什么想法吗?

【问题讨论】:

    标签: php symfony datetime doctrine-orm doctrine


    【解决方案1】:

    从值"2014-03-03"看来,您的数据库列类型似乎是date而不是datetime。

    运行php app/console doctrine:schema:update检查是否有变化。

    【讨论】:

    • 你说的很对!我收到了已经建好的数据库,但没有看到。谢谢我的朋友,你为我节省了宝贵的时间。
    【解决方案2】:

    我有同样的问题,我已经通过添加下面的代码来解决它 出现问题是因为 postgres 注册微秒时它与 0 不同,这会产生问题

    use Doctrine\DBAL\Types\Type;
    
    Type::overrideType('datetime', 'Doctrine\DBAL\Types\VarDateTimeType');
    Type::overrideType('datetimetz', 'Doctrine\DBAL\Types\VarDateTimeType');
    Type::overrideType('time', 'Doctrine\DBAL\Types\VarDateTimeType');
    
    Type::overrideType('datetime_immutable', 'Doctrine\DBAL\Types\VarDateTimeImmutableType');
    Type::overrideType('datetimetz_immutable', 'Doctrine\DBAL\Types\VarDateTimeImmutableType');
    Type::overrideType('time_immutable', 'Doctrine\DBAL\Types\VarDateTimeImmutableType');
    

    你可以在这里阅读更多 https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/known-vendor-issues.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2012-06-16
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      相关资源
      最近更新 更多