我想使用 Carbon我的 Symfony 3.2 应用程序中的对象而不是 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 配置中。
谢谢
最佳答案
哇....像往常一样,只要你提出问题,你就会找到解决方案:
doctrine:
dbal:
...
types:
datetime: DoctrineExtensions\Types\CarbonDateTimeType
date: DoctrineExtensions\Types\CarbonDateType
time: DoctrineExtensions\Types\CarbonTimeType
mapping_types:
enum: string
关于php - 在 Symfony3 中覆盖 Doctrine2 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953092/