Custom Mapping Types

Doctrine allows you to create new mapping types. This can come in handy when you’re missing a specific mapping type or when you want to replace the existing implementation of a mapping type.

Doctrine を使用すると、新しいマッピング タイプを作成できます。これは、特定のマッピング タイプが不足している場合や、マッピング タイプの既存の実装を置き換えたい場合に便利です。

In order to create a new mapping type you need to subclass Doctrine\DBAL\Types\Type and implement/override the methods as you wish. Here is an example skeleton of such a custom type class:

新しいマッピング タイプを作成するには、Doctrine\DBAL\Types\Type をサブクラス化し、必要に応じてメソッドを実装/オーバーライドする必要があります。このようなカスタム型クラスのスケルトンの例を次に示します。

<?php
namespace My\Project\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * My custom datatype.
 */
class MyType extends Type
{
    const MYTYPE = 'mytype'; // modify to match your type name

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        // return the SQL used to create your column type. To create a portable column type, use the $platform.
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        // This is executed when the value is read from the database. Make your conversions here, optionally using the $platform.
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        // This is executed when the value is written to the database. Make your conversions here, optionally using the $platform.
    }

    public function getName()
    {
        return self::MYTYPE; // modify to match your constant name
    }
}

Note

ノート

The following assumptions are applied to mapping types by the ORM:

ORM は、次の仮定をマッピング タイプに適用します。

  • The UnitOfWork never passes values to the database convert method that did not change in the request.

    UnitOfWork は、要求で変更されなかった値をデータベース convertmethod に渡すことはありません。

  • The UnitOfWork internally assumes that entity identifiers are castable to string. Hence, when using custom types that map to PHP objects as IDs, such objects must implement the __toString() magic method.

    UnitOfWork は、エンティティ識別子が文字列にキャスト可能であると内部的に想定しています。したがって、PHP オブジェクトに ID としてマップするカスタム型を使用する場合、そのようなオブジェクトは __toString() マジックメソッドを実装する必要があります。

When you have implemented the type you still need to let Doctrine know about it. This can be achieved through the Doctrine\DBAL\Types\Type#addType($name, $className) method. See the following example:

型を実装したら、それについて Doctrineknow に知らせる必要があります。これは、Doctrine\DBAL\Types\Type#addType($name, $className) メソッドで実現できます。次の例を参照してください。

<?php
// in bootstrapping code

// ...

use Doctrine\DBAL\Types\Type;

// ...

// Register my type
Type::addType('mytype', 'My\Project\Types\MyType');

To convert the underlying database type of your new “mytype” directly into an instance of MyType when performing schema operations, the type has to be registered with the database platform as well:

スキーマ操作を実行するときに、新しい「mytype」の基礎となるデータベース タイプを MyType のインスタンスに直接変換するには、タイプをデータベース プラットフォームにも登録する必要があります。

<?php
$conn = $em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('db_mytype', 'mytype');

When registering the custom types in the configuration you specify a unique name for the mapping type and map that to the corresponding fully qualified class name. Now the new type can be used when mapping columns:

構成にカスタム型を登録するときは、マッピング型に一意の名前を指定し、それを対応する完全修飾クラス名にマップします。列をマッピングするときに新しいタイプを使用できるようになりました。

<?php
class MyPersistentClass
{
    /** @Column(type="mytype") */
    private $field;
}

Previous topic

Aggregate Fields

集計フィールド

Next topic

Persisting the Decorator Pattern

デコレータ パターンの永続化

This Page

Fork me on GitHub