【发布时间】:2017-08-14 15:39:37
【问题描述】:
我想在我的 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 对象。它在 2 种情况下有效:
- 将实体更改为
@ORM\Column(type="carbondatetime") - 或者执行以下代码
\Doctrine\DBAL\Types\Type::overrideType('datetime', 'DoctrineExtensions\Types\CarbonDateTimeType');
是否有覆盖 Doctrine DBAL 类型的最佳实践?最好在 YAML 配置中。
谢谢
【问题讨论】:
标签: php symfony types doctrine-orm