php - 在 Symfony3 中覆盖 Doctrine2 类型

标签 php symfony types doctrine-orm

我想使用 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/

相关文章:

php - 这行不通?有什么建议么?

haskell - 类型构造函数可以与值构造函数相同吗?

function - 错误 - 推断类型不够通用

node.js - GraphQL类型解析field1列表记录,field2记录数量

javascript - 根据条件锁定和解锁

php - 为始终不同的变量插入语句

PHP计算执行时间,有时返回负值

php - 如何只匹配 Doctrine 中最近更新的记录?

symfony - 如何在symfony2的表单类型类中获取学说存储库?

PHP/Symfony : Why does a TwigExtension get a different service instance injected than a Controller?