我想在Symfony 3.2应用程序中使用Carbon个对象而不是SPL \ DateTime对象。我在here中找到了一组DoctrineExtension类。
编辑我的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');
覆盖 Doctrine DBAL类型是否有最佳实践?优选地,在YAML配置中。
由于
答案 0 :(得分:7)
哇哇......像往常一样,一旦你提出问题,你就会找到解决方案:
doctrine:
dbal:
...
types:
datetime: DoctrineExtensions\Types\CarbonDateTimeType
date: DoctrineExtensions\Types\CarbonDateType
time: DoctrineExtensions\Types\CarbonTimeType
mapping_types:
enum: string