2017-03-22 80 views
1

我想在我的Symfony 3.2应用程序中使用Carbon对象,而不是SPL \ DateTime对象。我在here中发现了一组DoctrineExtension类。覆盖Symfony3中的Doctrine2类型

编辑我config.yml文件:

doctrine: 
    dbal: 
     ... 
     types: 
      carbondatetime: DoctrineExtensions\Types\CarbonDateTimeType 
      carbondate: DoctrineExtensions\Types\CarbonDateType 
      carbontime: DoctrineExtensions\Types\CarbonTimeType 
     mapping_types: 
      datetime: carbondatetime 
      date: carbondate 
      enum: string 
      time: carbontime 

我成功地检查类型装有:

Doctrine\DBAL\Types\Type::getTypesMap() 

和映射,以及正常工作(返回carbondatetime):

$this->getDoctrine()->getManager() 
    ->getConnection()->getDatabasePlatform() 
    ->getDoctrineTypeMapping('datetime'); 

我对一个Doctrine存储库执行查询,仍然得到DateTime对象。它是工作在两种情况:

  • 更改实体到@ORM\Column(type="carbondatetime")
  • 或执行以下代码\Doctrine\DBAL\Types\Type::overrideType('datetime', 'DoctrineExtensions\Types\CarbonDateTimeType');

是否有一个最佳实践覆盖学说DBAL类型?最好在YAML配置中。

感谢

回答

4

哇....像往常一样,一旦你问的问题,你找到了解决方案:

doctrine: 
    dbal: 
     ... 
     types: 
      datetime: DoctrineExtensions\Types\CarbonDateTimeType 
      date: DoctrineExtensions\Types\CarbonDateType 
      time: DoctrineExtensions\Types\CarbonTimeType 
     mapping_types: 
      enum: string