2. Installation and Configuration

Doctrine can be installed with Composer.

Doctrine は Composer とともにインストールできます。

Define the following requirement in your composer.json file:

composer.json ファイルで次の要件を定義します。

{
    "require": {
        "doctrine/orm": "*"
    }
}

Then call composer install from your command line. If you don’t know how Composer works, check out their Getting Started to set up.

次に、コマンド ラインから composer install を呼び出します。 Composer の仕組みがわからない場合は、Getting Started を参照してセットアップしてください。

2.1. Class loading

Autoloading is taken care of by Composer. You just have to include the composer autoload file in your project:

オートローディングは Composer によって処理されます。プロジェクトに composer autoload ファイルを含めるだけです。

<?php
// bootstrap.php
// Include Composer Autoload (relative to project root).
require_once "vendor/autoload.php";

2.2. Obtaining an EntityManager

Once you have prepared the class loading, you acquire an EntityManager instance. The EntityManager class is the primary access point to ORM functionality provided by Doctrine.

クラスのロードを準備したら、EntityManager インスタンスを取得します。 EntityManager クラスは、Doctrine が提供する ORM 機能への主要なアクセス ポイントです。

<?php
// bootstrap.php
require_once "vendor/autoload.php";

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

$paths = ['/path/to/entity-files'];
$isDevMode = false;

// the connection configuration
$dbParams = [
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'foo',
];

$config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($dbParams, $config);
$entityManager = new EntityManager($connection, $config);

Note

ノート

The ORMSetup class has been introduced with ORM 2.12. It’s predecessor Setup is deprecated and will be removed in version 3.0.

ORMSetup クラスは ORM 2.12 で導入されました。前身の Setup は非推奨であり、バージョン 3.0 で削除されます。

Or if you prefer XML:

または、XML を使用する場合:

<?php
$paths = ['/path/to/xml-mappings'];
$config = ORMSetup::createXMLMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($dbParams, $config);
$entityManager = new EntityManager($connection, $config);

Or if you prefer YAML:

または、YAML を好む場合:

<?php
$paths = ['/path/to/yml-mappings'];
$config = ORMSetup::createYAMLMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($dbParams, $config);
$entityManager = new EntityManager($connection, $config);

Note

ノート

If you want to use yml mapping you should add yaml dependency to your composer.json:

yml マッピングを使用する場合は、yaml 依存関係を composer.json に追加する必要があります。

"symfony/yaml": "*"

Inside the ORMSetup methods several assumptions are made:

ORMSetup メソッド内では、いくつかの仮定が行われます。

  • If $isDevMode is true caching is done in memory with the ArrayAdapter. Proxy objects are recreated on every request.

    $isDevMode が true の場合、キャッシュは ArrayAdapter を使用してメモリ内で行われます。プロキシ オブジェクトは、リクエストごとに再作成されます。

  • If $isDevMode is false, check for Caches in the order APCu, Redis (127.0.0.1:6379), Memcache (127.0.0.1:11211) unless $cache is passed as fourth argument.

    $isDevMode が false の場合、$cache が 4 番目の引数として渡されない限り、APCu、Redis (127.0.0.1:6379)、Memcache (127.0.0.1:11211) の順にキャッシュをチェックします。

  • If $isDevMode is false, set then proxy classes have to be explicitly created through the command line.

    $isDevMode が false の場合、設定すると、コマンド ラインからプロキシ クラスを明示的に作成する必要があります。

  • If third argument $proxyDir is not set, use the systems temporary directory.

    3 番目の引数 $proxyDir が設定されていない場合は、システムの一時ディレクトリを使用します。

Note

ノート

In order to have ORMSetup configure the cache automatically, the library symfony/cache has to be installed as a dependency.

ORMSetup にキャッシュを自動的に設定させるには、ライブラリ symfony/cache を依存関係としてインストールする必要があります。

If you want to configure Doctrine in more detail, take a look at the Advanced Configuration section.

Doctrine をより詳細に設定したい場合は、高度な設定セクションをご覧ください。

Note

ノート

You can learn more about the database connection configuration in the Doctrine DBAL connection configuration reference.

データベース接続設定については、Doctrine DBAL 接続設定リファレンスを参照してください。

2.3. Setting up the Commandline Tool

Doctrine ships with a number of command line tools that are very helpful during development. In order to make use of them, create an executable PHP script in your project as described in the tools chapter.

Doctrine には、開発中に非常に役立つ多くのコマンドライン ツールが同梱されています。それらを利用するには、ツールの章で説明されているように、プロジェクトで実行可能な PHP スクリプトを作成します。

Table Of Contents

Previous topic

1. Architecture

1. 建築

Next topic

3. Frequently Asked Questions

3. よくある質問

This Page

Fork me on GitHub