26. Tools

26.1. Doctrine Console

The Doctrine Console is a Command Line Interface tool for simplifying common administration tasks during the development of a project that uses ORM.

Doctrine コンソールは、ORM を使用するプロジェクトの開発中に一般的な管理タスクを簡素化するためのコマンド ライン インターフェース ツールです。

For the following examples, we will set up the CLI as bin/doctrine.

以下の例では、CLI を bin/doctrine としてセットアップします。

26.1.1. Setting Up the Console

Whenever the doctrine command line tool is invoked, it can access all Commands that were registered by a developer. There is no auto-detection mechanism at work. The Doctrine binary already registers all the commands that currently ship with Doctrine DBAL and ORM. If you want to use additional commands you have to register them yourself.

doctrine コマンド ライン ツールが呼び出されるたびに、開発者によって登録されたすべてのコマンドにアクセスできます。動作中の自動検出メカニズムはありません。 Doctrineバイナリは、現在Doctrine DBALとORMに同梱されているすべてのコマンドをすでに登録しています。追加のコマンドを使用する場合は、それらを自分で登録する必要があります。

All the commands of the Doctrine Console require access to the EntityManager. You have to inject it into the console application.

Doctrine Console のすべてのコマンドは、エンティティマネージャーへのアクセスを必要とします。コンソール アプリケーションに挿入する必要があります。

Here is an example of a the project-specific bin/doctrine binary.

これは、プロジェクト固有の bin/doctrine バイナリの例です。

#!/usr/bin/env php
<?php

use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

// replace with path to your own project bootstrap file
require_once 'bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

$commands = [
    // If you want to add your own custom console commands,
    // you can do so here.
];

ConsoleRunner::run(
    new SingleManagerProvider($entityManager),
    $commands
);

Note

ノート

You have to adjust this snippet for your specific application or framework and use their facilities to access the Doctrine EntityManager and Connection Resources.

特定のアプリケーションまたはフレームワークに合わせてこのスニペットを調整し、それらの機能を使用して Doctrine EntityManager および接続リソースにアクセスする必要があります。

26.1.2. Display Help Information

Type php bin/doctrine on the command line and you should see an overview of the available commands or use the --help flag to get information on the available commands. If you want to know more about the use of generate entities for example, you can call:

コマンド ラインで php bin/doctrine と入力すると、使用可能なコマンドの概要が表示されるか、 --help フラグを使用して使用可能なコマンドに関する情報を取得できます。たとえば、エンティティの生成の使用について詳しく知りたい場合は、次のように呼び出すことができます。

$> php bin/doctrine orm:generate-entities --help

26.1.3. Command Overview

The following Commands are currently available:

現在、次のコマンドを使用できます。

  • help Displays help for a command (?)

    help コマンドのヘルプを表示します (?)

  • list Lists commands

    list コマンドを一覧表示します

  • dbal:import Import SQL file(s) directly to Database.

    dbal:import SQL ファイルをデータベースに直接インポートします。

  • dbal:run-sql Executes arbitrary SQL directly from the command line.

    dbal:run-sql コマンドラインから任意の SQL を直接実行します。

  • orm:clear-cache:metadata Clear all metadata cache of the various cache drivers.

    orm:clear-cache:metadata さまざまなキャッシュ ドライバのすべてのメタデータ キャッシュをクリアします。

  • orm:clear-cache:query Clear all query cache of the various cache drivers.

    orm:clear-cache:query さまざまなキャッシュ ドライバのすべてのクエリ キャッシュをクリアします。

  • orm:clear-cache:result Clear result cache of the various cache drivers.

    orm:clear-cache:result さまざまなキャッシュ ドライバの結果キャッシュをクリアします。

  • orm:convert-d1-schema Converts Doctrine 1.X schema into a Doctrine 2.X schema.

    orm:convert-d1-schema Doctrine 1.X スキーマを Doctrine 2.X スキーマに変換します。

  • orm:convert-mapping Convert mapping information between supported formats.

    orm:convert-mapping サポートされているフォーマット間でマッピング情報を変換します。

  • orm:ensure-production-settings Verify that Doctrine is properly configured for a production environment.

    orm:ensure-production-settings Doctrine が本番環境用に適切に設定されていることを確認します。

  • orm:generate-entities Generate entity classes and method stubs from your mapping information.

    orm:generate-entities マッピング情報からエンティティ クラスとメソッドタブを生成します。

  • orm:generate-proxies Generates proxy classes for entity classes.

    orm:generate-proxies エンティティ クラスのプロキシ クラスを生成します。

  • orm:generate-repositories Generate repository classes from your mapping information.

    orm:generate-repositories マッピング情報からリポジトリ クラスを生成します。

  • orm:run-dql Executes arbitrary DQL directly from the command line.

    orm:run-dql コマンドラインから任意の DQL を直接実行します。

  • orm:schema-tool:create Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.

    orm:schema-tool:create スキーマを処理し、EntityManager ストレージ接続で直接作成するか、SQL 出力を生成します。

  • orm:schema-tool:drop Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output.

    orm:schema-tool:drop スキーマを処理し、EntityManager Storage Connection のデータベース スキーマを削除するか、SQL 出力を生成します。

  • orm:schema-tool:update Processes the schema and either update the database schema of EntityManager Storage Connection or generate the SQL output.

    orm:schema-tool:update スキーマを処理し、EntityManager Storage Connection のデータベース スキーマを更新するか、SQL 出力を生成します。

For these commands are also available aliases:

これらのコマンドには、エイリアスも使用できます。

  • orm:convert:d1-schema is alias for orm:convert-d1-schema.

    orm:convert:d1-schema は orm:convert-d1-schema のエイリアスです。

  • orm:convert:mapping is alias for orm:convert-mapping.

    orm:convert:mapping は orm:convert-mapping のエイリアスです。

  • orm:generate:entities is alias for orm:generate-entities.

    orm:generate:entities は orm:generate-entities のエイリアスです。

  • orm:generate:proxies is alias for orm:generate-proxies.

    orm:generate:proxies は、orm:generate-proxies のエイリアスです。

  • orm:generate:repositories is alias for orm:generate-repositories.

    orm:generate:repositories は orm:generate-repositories のエイリアスです。

Note

ノート

Console also supports auto completion, for example, instead of orm:clear-cache:query you can use just o:c:q.

コンソールはオートコンプリートもサポートしています。たとえば、orm:clear-cache:query の代わりに o:c:q を使用できます。

26.2. Database Schema Generation

Note

ノート

SchemaTool can do harm to your database. It will drop or alter tables, indexes, sequences and such. Please use this tool with caution in development and not on a production server. It is meant for helping you develop your Database Schema, but NOT with migrating schema from A to B in production. A safe approach would be generating the SQL on development server and saving it into SQL Migration files that are executed manually on the production server.

SchemaTool はデータベースに害を及ぼす可能性があります。変更可能、インデックス、シーケンスなどを削除または変更します。このツールは、運用サーバーではなく、開発段階で注意して使用してください。これは、データベース スキーマの開発を支援するためのものですが、本番環境でスキーマを A から B に移行するためのものではありません。安全な方法は、開発サーバーで SQL を生成し、それを SQLMigration ファイルに保存して、運用サーバーで手動で実行することです。

SchemaTool assumes your Doctrine Project uses the given database on its own. Update and Drop commands will mess with other tables if they are not related to the current project that is using Doctrine. Please be careful!

SchemaTool は、Doctrine プロジェクトが指定されたデータベースを独自に使用していると想定します。 Update および Drop コマンドは、Doctrine を使用している現在のプロジェクトに関連していない場合、他のテーブルを混乱させます。注意してください!

To generate your database schema from your Doctrine mapping files you can use the SchemaTool class or the schema-tool Console Command.

Doctrine マッピング ファイルからデータベース スキーマを生成するには、SchemaTool クラスまたは schema-tool ConsoleCommand を使用できます。

When using the SchemaTool class directly, create your schema using the createSchema() method. First create an instance of the SchemaTool and pass it an instance of the EntityManager that you want to use to create the schema. This method receives an array of ClassMetadata instances.

SchemaTool クラスを直接使用する場合は、createSchema() メソッドを使用してスキーマを作成します。最初に SchemaTool のインスタンスを作成し、スキーマの作成に使用する EntityManager のインスタンスを渡します。このメソッドは、ClassMetadata インスタンスの配列を受け取ります。

<?php
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
  $em->getClassMetadata('Entities\User'),
  $em->getClassMetadata('Entities\Profile')
);
$tool->createSchema($classes);

To drop the schema you can use the dropSchema() method.

スキーマを削除するには、dropSchema() メソッドを使用できます。

<?php
$tool->dropSchema($classes);

This drops all the tables that are currently used by your metadata model. When you are changing your metadata a lot during development you might want to drop the complete database instead of only the tables of the current model to clean up with orphaned tables.

これにより、メタデータモデルで現在使用されているすべてのテーブルが削除されます。開発中にメタデータを頻繁に変更する場合は、現在のモデルのテーブルだけでなく、データベース全体を削除して、孤立したテーブルでクリーンアップすることをお勧めします。

<?php
$tool->dropSchema($classes, \Doctrine\ORM\Tools\SchemaTool::DROP_DATABASE);

You can also use database introspection to update your schema easily with the updateSchema() method. It will compare your existing database schema to the passed array of ClassMetadata instances.

updateSchema() メソッドを使用して、データベースのイントロスペクションを使用してスキーマを簡単に更新することもできます。既存のデータベース スキーマを渡された ClassMetadata インスタンスの配列と比較します。

<?php
$tool->updateSchema($classes);

If you want to use this functionality from the command line you can use the schema-tool command.

コマンド ラインからこの機能を使用する場合は、schema-tool コマンドを使用できます。

To create the schema use the create command:

スキーマを作成するには、create コマンドを使用します。

$ php bin/doctrine orm:schema-tool:create

To drop the schema use the drop command:

スキーマを削除するには、drop コマンドを使用します。

$ php bin/doctrine orm:schema-tool:drop

If you want to drop and then recreate the schema then use both options:

スキーマを削除してから再作成する場合は、両方のオプションを使用します。

$ php bin/doctrine orm:schema-tool:drop
$ php bin/doctrine orm:schema-tool:create

As you would think, if you want to update your schema use the update command:

ご想像のとおり、スキーマを更新する場合は、update コマンドを使用します。

$ php bin/doctrine orm:schema-tool:update

All of the above commands also accept a --dump-sql option that will output the SQL for the ran operation.

上記のすべてのコマンドは、実行された操作の SQL を出力する --dump-sql オプションも受け入れます。

$ php bin/doctrine orm:schema-tool:create --dump-sql

26.3. Entity Generation

Generate entity classes and method stubs from your mapping information.

マッピング情報からエンティティ クラスとメソッド スタブを生成します。

$ php bin/doctrine orm:generate-entities
$ php bin/doctrine orm:generate-entities --update-entities
$ php bin/doctrine orm:generate-entities --regenerate-entities

This command is not suited for constant usage. It is a little helper and does not support all the mapping edge cases very well. You still have to put work in your entities after using this command.

このコマンドは、常時使用するのには適していません。これは小さなヘルパーであり、すべてのマッピング エッジ ケースをうまくサポートしているわけではありません。このコマンドを使用した後も、エンティティに作業を入れる必要があります。

It is possible to use the EntityGenerator on code that you have already written. It will not be lost. The EntityGenerator will only append new code to your file and will not delete the old code. However this approach may still be prone to error and we suggest you use code repositories such as GIT or SVN to make backups of your code.

すでに記述したコードで EntityGenerator を使用することができます。失われることはありません。 EntityGenerator は、新しいコードをファイルに追加するだけで、古いコードは削除しません。ただし、この方法でもエラーが発生する可能性があるため、GIT や SVN などのコード リポジトリを使用してコードのバックアップを作成することをお勧めします。

It makes sense to generate the entity code if you are using entities as Data Access Objects only and don’t put much additional logic on them. If you are however putting much more logic on the entities you should refrain from using the entity-generator and code your entities manually.

エンティティを DataAccess オブジェクトとしてのみ使用し、追加のロジックを追加しない場合は、エンティティ コードを生成するのが理にかなっています。ただし、エンティティにさらに多くのロジックを追加する場合は、エンティティ ジェネレータの使用を控え、エンティティを手動でコーディングする必要があります。

Note

ノート

Even if you specified Inheritance options in your XML or YAML Mapping files the generator cannot generate the base and child classes for you correctly, because it doesn’t know which class is supposed to extend which. You have to adjust the entity code manually for inheritance to work!

XML または YAML マッピング ファイルで継承オプションを指定した場合でも、ジェネレーターは基本クラスと子クラスを正しく生成できません。継承が機能するには、エンティティコードを手動で調整する必要があります!

26.4. Convert Mapping Information

Convert mapping information between supported formats.

サポートされている形式間でマッピング情報を変換します。

This is an execute one-time command. It should not be necessary for you to call this method multiple times, especially when using the --from-database flag.

これは 1 回限りの実行コマンドです。 --from-database フラグを使用する場合は特に、このメソッドを複数回呼び出す必要はありません。

Converting an existing database schema into mapping files only solves about 70-80% of the necessary mapping information. Additionally the detection from an existing database cannot detect inverse associations, inheritance types, entities with foreign keys as primary keys and many of the semantical operations on associations such as cascade.

既存のデータベース スキーマをマッピング ファイルに変換しても、必要なマッピング情報の約 70 ~ 80% しか解決されません。さらに、既存のデータベースからの検出では、逆関連付け、継承タイプ、主キーとして外部キーを持つエンティティ、およびカスケードなどの関連付けに対するこれらの多くの操作を検出できません。

Note

ノート

There is no need to convert YAML or XML mapping files to annotations every time you make changes. All mapping drivers are first class citizens in Doctrine 2 and can be used as runtime mapping for the ORM. See the docs on XML and YAML Mapping for an example how to register this metadata drivers as primary mapping source.

変更を加えるたびに、YAML または XML マッピング ファイルを注釈に変換する必要はありません。すべてのマッピングドライバーは Doctrine 2 の第一級市民であり、ORM のランタイムマッピングとして使用できます。このメタデータ ドライバーをプライマリ マッピング ソースとして登録する方法の例については、XML および YAML マッピングに関するドキュメントを参照してください。

To convert some mapping information between the various supported formats you can use the ClassMetadataExporter to get exporter instances for the different formats:

サポートされているさまざまな形式間でマッピング情報を変換するには、ClassMetadataExporter を使用して、さまざまな形式のエクスポーター インスタンスを取得できます。

<?php
$cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();

Once you have a instance you can use it to get an exporter. For example, the yml exporter:

インスタンスを取得したら、それを使用してエクスポーターを取得できます。たとえば、yml エクスポーターは次のようになります。

<?php
$exporter = $cme->getExporter('yml', '/path/to/export/yml');

Now you can export some ClassMetadata instances:

これで、いくつかの ClassMetadata インスタンスをエクスポートできます。

<?php
$classes = array(
  $em->getClassMetadata('Entities\User'),
  $em->getClassMetadata('Entities\Profile')
);
$exporter->setMetadata($classes);
$exporter->export();

This functionality is also available from the command line to convert your loaded mapping information to another format. The orm:convert-mapping command accepts two arguments, the type to convert to and the path to generate it:

この機能はコマンド ラインからも利用でき、ロードしたマッピング情報を別の形式に変換できます。 Theorm:convert-mapping コマンドは、変換先の型とそれを生成するパスの 2 つの引数を受け取ります。

$ php bin/doctrine orm:convert-mapping xml /path/to/mapping-path-converted-to-xml

26.5. Reverse Engineering

You can use the DatabaseDriver to reverse engineer a database to an array of ClassMetadata instances and generate YAML, XML, etc. from them.

DatabaseDriver を使用して、データベースをリバース エンジニアリングして ClassMetadata インスタンスの配列にし、そこから YAML や XML などを生成できます。

Note

ノート

Reverse Engineering is a one-time process that can get you started with a project. Converting an existing database schema into mapping files only detects about 70-80% of the necessary mapping information. Additionally the detection from an existing database cannot detect inverse associations, inheritance types, entities with foreign keys as primary keys and many of the semantical operations on associations such as cascade.

リバース エンジニアリングは、プロジェクトを開始できる 1 回限りのプロセスです。既存のデータベース スキーマをマッピング ファイルに変換しても、必要なマッピング情報の約 70 ~ 80% しか検出されません。さらに、既存のデータベースからの検出では、逆関連付け、継承タイプ、主キーとして外部キーを持つエンティティ、およびカスケードなどの関連付けに対するこれらの多くの操作を検出できません。

First you need to retrieve the metadata instances with the DatabaseDriver:

最初に、DatabaseDriver を使用してメタデータ インスタンスを取得する必要があります。

<?php
$em->getConfiguration()->setMetadataDriverImpl(
    new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
        $em->getConnection()->getSchemaManager()
    )
);

$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$metadata = $cmf->getAllMetadata();

Now you can get an exporter instance and export the loaded metadata to yml:

これで、エクスポーター インスタンスを取得し、読み込まれたメタデータを yml にエクスポートできます。

<?php
$cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
$exporter = $cme->getExporter('yml', '/path/to/export/yml');
$exporter->setMetadata($metadata);
$exporter->export();

You can also reverse engineer a database using the orm:convert-mapping command:

theorm:convert-mapping コマンドを使用して、データベースをリバース エンジニアリングすることもできます。

$ php bin/doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml

Note

ノート

Reverse Engineering is not always working perfectly depending on special cases. It will only detect Many-To-One relations (even if they are One-To-One) and will try to create entities from Many-To-Many tables. It also has problems with naming of foreign keys that have multiple column names. Any Reverse Engineered Database-Schema needs considerable manual work to become a useful domain model.

特殊なケースによっては、リバース エンジニアリングが常に完全に機能するとは限りません。多対一の関係のみを検出し (一対一であっても)、多対多のテーブルからエンティティを作成しようとします。また、複数の列名を持つ外部キーの命名にも問題があります。リバース エンジニアリングされたデータベース スキーマが有用なドメイン モデルになるには、かなりの手作業が必要です。

26.6. Runtime vs Development Mapping Validation

For performance reasons Doctrine ORM has to skip some of the necessary validation of metadata mappings. You have to execute this validation in your development workflow to verify the associations are correctly defined.

パフォーマンス上の理由から、Doctrine ORM はメタデータ マッピングの必要な検証の一部をスキップする必要があります。関連付けが正しく定義されていることを確認するには、開発ワークフローでこの検証を実行する必要があります。

You can either use the Doctrine Command Line Tool:

Doctrine Command Line Tool を使用することができます:

doctrine orm:validate-schema

If the validation fails, you can change the verbosity level to check the detected errors:

検証が失敗した場合は、詳細レベルを変更して、検出されたエラーを確認できます。

doctrine orm:validate-schema -v

doctrine orm:validate-schema -v

Or you can trigger the validation manually:

または、検証を手動でトリガーできます。

<?php
use Doctrine\ORM\Tools\SchemaValidator;

$validator = new SchemaValidator($entityManager);
$errors = $validator->validateMapping();

if (count($errors) > 0) {
    // Lots of errors!
    echo implode("\n\n", $errors);
}

If the mapping is invalid the errors array contains a positive number of elements with error messages.

マッピングが無効な場合、エラー配列には、エラー メッセージを含む正の数の要素が含まれます。

Warning

警告

One mapping option that is not validated is the use of the referenced column name. It has to point to the equivalent primary key otherwise Doctrine will not work.

検証されていないマッピング オプションの 1 つは、参照された列名の使用です。同等の主キーを指す必要があります。そうしないと、Doctrine は機能しません。

Note

ノート

One common error is to use a backlash in front of the fully-qualified class-name. Whenever a FQCN is represented inside a string (such as in your mapping definitions) you have to drop the prefix backslash. PHP does this with get_class() or Reflection methods for backwards compatibility reasons.

よくある間違いの 1 つは、完全修飾クラス名の前にバックラッシュを使用することです。 FQCN が文字列内 (マッピング定義など) で表される場合は常に、プレフィックスのバックスラッシュを削除する必要があります。 PHP は、下位互換性のために get_class() または Reflectionmethods を使用してこれを行います。

26.7. Adding own commands

You can also add your own commands on-top of the Doctrine supported tools if you are using a manually built console script.

手動で作成したコンソール スクリプトを使用している場合は、Doctrine がサポートするツールの上に独自のコマンドを追加することもできます。

To include a new command on Doctrine Console, you need to do modify the doctrine.php file a little:

Doctrine コンソールに新しいコマンドを含めるには、doctrine.php ファイルを少し変更する必要があります。

<?php
// doctrine.php
use Symfony\Component\Console\Application;

// as before ...

// replace the ConsoleRunner::run() statement with:
$cli = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);

// Register All Doctrine Commands
ConsoleRunner::addCommands($cli);

// Register your own command
$cli->addCommand(new \MyProject\Tools\Console\Commands\MyCustomCommand);

// Runs console application
$cli->run();

Additionally, include multiple commands (and overriding previously defined ones) is possible through the command:

さらに、次のコマンドを使用して、複数のコマンドを含める (および以前に定義したコマンドをオーバーライドする) ことができます。

<?php

$cli->addCommands(array(
    new \MyProject\Tools\Console\Commands\MyCustomCommand(),
    new \MyProject\Tools\Console\Commands\SomethingCommand(),
    new \MyProject\Tools\Console\Commands\AnotherCommand(),
    new \MyProject\Tools\Console\Commands\OneMoreCommand(),
));

26.8. Re-use console application

You are also able to retrieve and re-use the default console application. Just call ConsoleRunner::createApplication(...) with an appropriate HelperSet, like it is described in the configuration section.

また、デフォルトのコンソール アプリケーションを取得して再利用することもできます。構成セクションで説明されているように、適切な HelperSet を指定して ConsoleRunner::createApplication(...) を呼び出すだけです。

<?php

// Retrieve default console application
$cli = ConsoleRunner::createApplication($helperSet);

// Runs console application
$cli->run();

Table Of Contents

Previous topic

25. Improving Performance

25. パフォーマンスの向上

Next topic

27. Metadata Drivers

27. メタデータドライバー

This Page

Fork me on GitHub