簡體   English   中英

表和列名稱的命名策略(Doctrine ORM)

[英]Naming Strategy for table and column names (Doctrine ORM)

有一種命名策略可以照顧到Doctrine ORM中的表和列名的映射嗎?

現在,所有名稱都是通過實體類中的注釋指定的,例如

<?php

namespace App\Entity;

use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="role")
 */
class Role
{
    /**
     * @ORM\Id()
     * @ORM\Column(name="id", type="guid")
     * @ORM\GeneratedValue(strategy="NONE")
     * @var string
     */
    private $id;

    /**
     * @ORM\Column(name="created_at", type="datetime")
     * @var \DateTimeImmutable
     */
    private $createdAt;

    /**
     * @ORM\Column(name="created_by", type="string")
     * @var string
     */
    private $createdBy;

    // [..]

}

表和列的名稱均為snake_case而類和屬性的名稱均為camelCase

我試圖刪除實體類中的表名和列名聲明,並通過配置提供了命名策略,並嘗試通過以下兩種方式進行設置。

<?php

use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;

return [
    'doctrine' => [
        'connection' => [
            // [..]
        ],
        'driver' => [
            // [..]
        ],
        'annotation' => [
            // [..]
        ],
        'entity_managers' => [
            'default' => [
                'naming_strategy' => UnderscoreNamingStrategy::class,
            ],
        ],
        'orm' => [
            'naming_strategy' => UnderscoreNamingStrategy::class,
        ],
    ],
];

嘗試檢索實體時,將引發錯誤。

Doctrine\DBAL\Exception\InvalidFieldNameException: An exception occurred while executing &#039;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&#039;:

SQLSTATE[42S22]: Column not found: 1054 Unknown column &#039;t0.createdAt&#039; in &#039;field list&#039; in file C:\project\path\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php on line 60

經過一些研究和試驗,我得到了以下解決方案,可以在Zend Expressive應用程序中工作。

doctrine.local.php中配置命名策略

<?php

declare(strict_types = 1);

use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;

return [
    'doctrine' => [
        'connection' => [
            // [..]
        ],
        'driver' => [
            // [..]
        ],
        'annotation' => [
            // [..]
        ],
        'configuration' => [
            'orm_default' => [
                'naming_strategy' => UnderscoreNamingStrategy::class,
            ],
        ],
    ],
];

為命名策略實施工廠

<?php

namespace App;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class NamingStrategyFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        return new $requestedName();
    }
}

ConfigProvider.php中注冊工廠

<?php

declare(strict_types = 1);

namespace App;

use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;

class ConfigProvider
{
    public function __invoke()
    {
        return [
            'dependencies' => $this->getDependencies(),
        ];
    }

    public function getDependencies(): array
    {
        return [
            'invokables' => [
            ],
            'factories' => [
                // [..]
                UnderscoreNamingStrategy::class => NamingStrategyFactory::class,
            ],
        ];
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM