2017-03-22 77 views
1

我想在我的Symfony 3.2應用程序中使用Carbon對象,而不是SPL \ DateTime對象。我在here中發現了一組DoctrineExtension類。覆蓋Symfony3中的Doctrine2類型

編輯我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');

是否有一個最佳實踐覆蓋學說DBAL類型?最好在YAML配置中。

感謝

回答

4

哇....像往常一樣,一旦你問的問題,你找到了解決方案:

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