在Symfony3中覆盖Doctrine2类型 [英] Override Doctrine2 types in Symfony3

查看:37
本文介绍了在Symfony3中覆盖Doctrine2类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Symfony 3.2应用程序中使用 Carbon 对象而不是SPL \ DateTime目的.我在此处中找到了一组DoctrineExtension类.

I would like to use Carbon objects in my Symfony 3.2 application instead of the SPL \DateTime object. I found a set of DoctrineExtension classes in here.

编辑了我的config.yml文件:

Edited my config.yml file:

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

我成功检查了类型是否已加载:

I successfully check if the types are loaded with:

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

映射也正常工作(返回 carbondatetime ):

And the mapping as well is working properly (returns carbondatetime):

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

我在Doctrine存储库上执行查询,但仍然获得DateTime对象.它在2种情况下有效:

I execute a query over a Doctrine repository and still get DateTime objects. It is working in 2 cases:

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

是否存在用于替代 Doctrine DBAL类型的最佳实践?最好是在YAML配置中.

Is there a Best-Practice to override the Doctrine DBAL types? Preferably in the YAML configuration.

谢谢

推荐答案

哇....像往常一样,只要您问问题就可以找到解决方法:

Woah.... Like always, as soon you ask the question you find the solution:

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

这篇关于在Symfony3中覆盖Doctrine2类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆