是否有可能让命名策略负责在 Doctrine ORM 中映射表名和列名?
现在,所有名称都通过实体类中的注释指定,例如
<?php
namespace AppEntity;
use DateTimeInterface;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity()
* @ORMTable(name="role")
*/
class Role
{
/**
* @ORMId()
* @ORMColumn(name="id", type="guid")
* @ORMGeneratedValue(strategy="NONE")
* @var string
*/
private $id;
/**
* @ORMColumn(name="created_at", type="datetime")
* @var DateTimeImmutable
*/
private $createdAt;
/**
* @ORMColumn(name="created_by", type="string")
* @var string
*/
private $createdBy;
// [..]
}
表和列名称都是snake_case
而类和属性名称都是camelCase
的。
我尝试删除实体类中的表和列名称声明,并通过配置提供了命名策略,尝试以以下两种方式进行设置。
<?php
use DoctrineORMMappingUnderscoreNamingStrategy;
return [
'doctrine' => [
'connection' => [
// [..]
],
'driver' => [
// [..]
],
'annotation' => [
// [..]
],
'entity_managers' => [
'default' => [
'naming_strategy' => UnderscoreNamingStrategy::class,
],
],
'orm' => [
'naming_strategy' => UnderscoreNamingStrategy::class,
],
],
];
尝试检索实体时,会引发错误。
DoctrineDBALExceptionInvalidFieldNameException: An exception occurred while executing 'SELECT t0.id AS id_1, t0.createdAt AS createdAt_2, t0.createdBy AS createdBy_3, t0.updatedAt AS updatedAt_4, t0.updatedBy AS updatedBy_5, t0.name AS name_6, t0.desc AS desc_7, t0.isCore AS isCore_8 FROM Role t0':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.createdAt' in 'field list' in file C:projectpathvendordoctrinedballibDoctrineDBALDriverAbstractMySQLDriver.php on line 60
经过一些研究和实验,我得到了以下解决方案,可以在Zend Expressive应用程序中工作。
在 doctrine.local 中配置命名策略.php
<?php
declare(strict_types = 1);
use DoctrineORMMappingUnderscoreNamingStrategy;
return [
'doctrine' => [
'connection' => [
// [..]
],
'driver' => [
// [..]
],
'annotation' => [
// [..]
],
'configuration' => [
'orm_default' => [
'naming_strategy' => UnderscoreNamingStrategy::class,
],
],
],
];
为命名策略实现工厂
<?php
namespace App;
use InteropContainerContainerInterface;
use ZendServiceManagerFactoryFactoryInterface;
class NamingStrategyFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new $requestedName();
}
}
在配置提供程序中注册工厂.php
<?php
declare(strict_types = 1);
namespace App;
use DoctrineORMMappingUnderscoreNamingStrategy;
class ConfigProvider
{
public function __invoke()
{
return [
'dependencies' => $this->getDependencies(),
];
}
public function getDependencies(): array
{
return [
'invokables' => [
],
'factories' => [
// [..]
UnderscoreNamingStrategy::class => NamingStrategyFactory::class,
],
];
}
}