32. Implementing a NamingStrategy

Using a naming strategy you can provide rules for generating database identifiers, column or table names. This feature helps reduce the verbosity of the mapping document, eliminating repetitive noise (eg: TABLE_).

命名戦略を使用して、データベース識別子、列またはテーブル名を生成するためのルールを提供できます。この機能は、マッピング ドキュメントの冗長性を減らし、繰り返しのノイズ (TABLE_ など) を排除するのに役立ちます。

32.1. Configuring a naming strategy

The default strategy used by Doctrine is quite minimal.

Doctrine が使用するデフォルトの戦略は非常に最小限です。

By default the Doctrine\ORM\Mapping\DefaultNamingStrategy uses the simple class name and the attribute names to generate tables and columns.

デフォルトでは、Doctrine\ORM\Mapping\DefaultNamingStrategy は単純なクラス名と属性名を使用してテーブルと列を生成します。

You can specify a different strategy by calling Doctrine\ORM\Configuration#setNamingStrategy():

Doctrine\ORM\Configuration#setNamingStrategy() を呼び出して、別の戦略を指定できます。

<?php
$namingStrategy = new MyNamingStrategy();
$configuration->setNamingStrategy($namingStrategy);

32.2. Underscore naming strategy

\Doctrine\ORM\Mapping\UnderscoreNamingStrategy is a built-in strategy.

\Doctrine\ORM\Mapping\UnderscoreNamingStrategy は組み込みの戦略です。

<?php
$namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_UPPER);
$configuration->setNamingStrategy($namingStrategy);

For SomeEntityName the strategy will generate the table SOME_ENTITY_NAME with the CASE_UPPER option, or some_entity_name with the CASE_LOWER option.

SomeEntityName の場合、戦略は、CASE_UPPER オプションを使用してテーブル SOME_ENTITY_NAME を生成するか、CASE_LOWER オプションを使用して some_entity_name を生成します。

32.3. Naming strategy interface

The interface Doctrine\ORM\Mapping\NamingStrategy allows you to specify a naming strategy for database tables and columns.

インターフェイス Doctrine\ORM\Mapping\NamingStrategy を使用すると、データベースのテーブルと列の命名戦略を指定できます。

<?php
/**
 * Return a table name for an entity class
 *
 * @param string $className The fully-qualified class name
 * @return string A table name
 */
function classToTableName($className);

/**
 * Return a column name for a property
 *
 * @param string $propertyName A property
 * @return string A column name
 */
function propertyToColumnName($propertyName);

/**
 * Return the default reference column name
 *
 * @return string A column name
 */
function referenceColumnName();

/**
 * Return a join column name for a property
 *
 * @param string $propertyName A property
 * @return string A join column name
 */
function joinColumnName($propertyName, $className = null);

/**
 * Return a join table name
 *
 * @param string $sourceEntity The source entity
 * @param string $targetEntity The target entity
 * @param string $propertyName A property
 * @return string A join table name
 */
function joinTableName($sourceEntity, $targetEntity, $propertyName = null);

/**
 * Return the foreign key column name for the given parameters
 *
 * @param string $entityName A entity
 * @param string $referencedColumnName A property
 * @return string A join column name
 */
function joinKeyColumnName($entityName, $referencedColumnName = null);

32.4. Implementing a naming strategy

If you have database naming standards, like all table names should be prefixed by the application prefix, all column names should be lower case, you can easily achieve such standards by implementing a naming strategy.

すべてのテーブル名の前にアプリケーション プレフィックスを付ける必要がある、すべての列名を小文字にするなど、データベースの命名基準がある場合は、命名戦略を実装することでそのような基準を簡単に達成できます。

You need to create a class which implements Doctrine\ORM\Mapping\NamingStrategy.

Doctrine\ORM\Mapping\NamingStrategy を実装するクラスを作成する必要があります。

<?php
class MyAppNamingStrategy implements NamingStrategy
{
    public function classToTableName($className)
    {
        return 'MyApp_' . substr($className, strrpos($className, '\\') + 1);
    }
    public function propertyToColumnName($propertyName)
    {
        return $propertyName;
    }
    public function referenceColumnName()
    {
        return 'id';
    }
    public function joinColumnName($propertyName, $className = null)
    {
        return $propertyName . '_' . $this->referenceColumnName();
    }
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
    {
        return strtolower($this->classToTableName($sourceEntity) . '_' .
                $this->classToTableName($targetEntity));
    }
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
    {
        return strtolower($this->classToTableName($entityName) . '_' .
                ($referencedColumnName ?: $this->referenceColumnName()));
    }
}

Table Of Contents

Previous topic

31. Filters

31. フィルター

Next topic

33. Advanced Configuration

33.高度な設定

This Page

Fork me on GitHub