The symfony and Doctrine book

2章 - 接続

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "The symfony and Doctrine book" which is licensed under the GFDL license.

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com

Chapter Content

導入

サポートされるドライバ

データソース名(DSN)

Doctrineスタイル

PDOスタイル

データベースからインポートする

並列接続

接続属性

すべてをビルドする

symfony training
Be trained by symfony experts
May 29: Paris (Web Development with Symfony2 - Français)
May 31: Paris (Mastering Symfony2 - Français)
Jun 06: Paris (Introduction to Symfony2 - Français)
Jun 06: Paris (Introduction to Symfony2 - English)
Jun 06: Paris (Going Further with Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "The symfony and Doctrine book" in Japanese for the 1.2 version - Switch to language:
This work is licensed under a GFDL license.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

導入

この章では複数の接続の設定する、モデルをバインドする、活動に関連するデータベースと他の接続の作成と削除の方法など、Doctrineの接続についていくつか説明します。

デフォルトのconfig/databases.ymlは次のようになります。

all:
  propel:
    class:      sfPropelDatabase
    param:
      dsn:      mysql:host=localhost;dbname=dbname
      username: user

PropelとDoctrineの唯一の違いはsfPropelDatabaseの代わりにsfDoctrineDatabaseで接続名はpropelの代わりにdoctrineでなければならないことです。 DoctrineとPropelの両方はPDO(PHP Data Objects)をデータベース抽象化レイヤーとして使います。

Propelはpropelという名前の接続を少なくとも1つ必要ですが、Doctrineはdoctrineという名前の接続を必要としないので好きな名前を使うことができます。

次のようにconfigure:databaseタスクでconfig/databases.ymlの中の接続を設定できます。

$ ./symfony configure:database --name=doctrine --class=sfDoctrineDatabase "mysql:host=localhost;dbname=dbname" user secret

次のような新しい接続の設定を見ることになります:

doctrine:
  class: sfDoctrineDatabase
  param:
    dsn: 'mysql:host=localhost;dbname=dbname'
    username: user
    password: secret

sfPropelPluginを無効にすればconfig/databases.ymlのpropelへの参照を完全に削除する必要があります。

サポートされるドライバ

DoctrineはPDOがサポートするすべてのドライバをサポートします。PHPにおいてPDOと使いたいPDO_* ドライバの両方をコンパイルしなければなりません。PDOと連携するデータベースの一覧は次の通りです。

名前 説明
MS SQL Server Microsoft SQL ServerとSybase関数 (PDO_DBLIB)
Firebird/Interbase Firebird/Interbase関数 (PDO_FIREBIRD)
IBM IBM関数 (PDO_IBM)
Informix Informix関数 (PDO_INFORMIX)
MySQL MySQL関数 (PDO_MYSQL)
Oracle Oracle関数 (PDO_OCI)
ODBC and DB2 ODBCとDB2関数 (PDO_ODBC)
PostgreSQL PostgreSQL関数 (PDO_PGSQL)
SQLite SQLite関数 (PDO_SQLITE)

PDOの詳細は http://www.php.net/pdo で読むことができます。

データソース名(DSN)

DoctrineはDSN(Data Source Name)の情報を指定する方法を2つ提供します。DoctrineスタイルのDSNもしくはネイティブのPDOスタイルの両方を利用できます。

Doctrineスタイル

DoctrineはPEARのMDB2パッケージを参考にしたDSN構文を持ちます。

all:
  doctrine:
    class:          sfDoctrineDatabase
    param:
      dsn:          driver://username:password@host/database_name

PDOスタイル

PDO構文でもDSN情報を指定できます。

all:
  doctrine:
    class:          sfDoctrineDatabase
    param:
      dsn:          driver:dbname=database_name;host=localhost
      username:     username
      password:     password

PDOスタイルの構文を利用することでより優れた柔軟性とPDOへの接続に関する非標準の情報を指定できます。 たとえば、接続する際に非標準のunix_socketのパスもしくはポートをPDO構文で指定すればより柔軟に対応できます。>configure:databaseコマンドはPDOスタイルでのみ動作します。

データベースからインポートする

Doctrineは既存のデータベースからconfig/doctrine/schema.ymlスキーマファイルを生成する機能を持ちます。 インポートしたいデータベースに対してDoctrineの接続を設定して次のコマンドを走らせるだけです。

これはPropelスキーマをDoctrineのものに変換するよい方法です。 Propelを利用してデータベースを作成し、Doctrineを利用して作成されたデータベースからスキーマを生成します。

$ ./symfony doctrine:build-schema
>> doctrine  generating yaml schema from database

config/doctrine/schema.ymlの中を見るとデータベースに関するYAML形式の情報が見つかります。 この例ではユーザーテーブルが1つあります。

CREATE TABLE user (id BIGINT AUTO_INCREMENT, username VARCHAR(255), password VARCHAR(255), PRIMARY KEY(id)) ENGINE = INNODB;

上記のMySQLのテーブルはconfig/doctrine/schema.ymlの中で次のようなYAML形式のスキーマを生成します。

User:
  tableName: user
  columns:
    id:
      type: integer(8)
      primary: true
      autoincrement: true
    username: string(255)
    password: string(255)

並列接続

Doctrineは並列接続をする機能を提供します。クエリが適切な接続の上で実行されるようにモデルを接続に簡単にバインドできます。 最初にconfigure:databaseコマンドで並列接続を追加する必要があります。

$ ./symfony configure:database --name=master --class=sfDoctrineDatabase "mysql:host=localhost;dbname=master" user secret
$ ./symfony configure:database --name=client --class=sfDoctrineDatabase "mysql:host=localhost;dbname=client" user secret

作成したオリジナルの接続を削除するとconfig/databases.ymlは次のようになります。

all:
  master:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=localhost;dbname=master'
      username: user
      password: secret
  client:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=localhost;dbname=client'
      username: user
      password: secret

マスターデータベースにバインドしたいClientモデルがあるとします。 次のようにconfig/doctrine/schema.ymlにYAMLコードを設定することで、モデルを直接定義できます。

Client:
  connection: master
  columns:
    name: string(255)
    username: string(255)
    password: string(255)

それぞれのClientStoresを持ちますがこれらはClientsから個別のデータベースに保存されます。

Store:
  connection: client
  attributes:
    export: tables
  columns:
    name: string(255)
    description: string(500)
    client_id: integer
  relations:
    Client:
     foreignAlias: Stores

テーブルは個別のデータベースの中にあるのでデータは遅延ロードのみできます。 現在DoctrineはデータベースをまたがったテーブルをjoinするSQLの生成をサポートしません。 また、テーブルに設定されたexport属性に注意してください。 これはCREATE TABLE文だけをエクスポートして外部キーの制約はエクスポートしないようにDoctrineに指示します。

接続属性

sfDoctrinePluginによって次のようにconfig/databases.ymlファイルの中で接続属性を直に指定できます。

  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=localhost;dbname=dbname'
      username: user
      password: secret
      attributes:
        use_dql_callbacks: true

ここで指定した属性は接続が作成されたときにDoctrine_Connectionインスタンスに設定されます。

Doctrineの属性は機能の設定と制御のためにあります。 属性の詳細は Doctrineの公式ドキュメントで読むことができます。

すべてをビルドする

定義した接続とスキーマは次のコマンドですべてビルドできます。

$ ./symfony doctrine:build-all-reload

  This command will remove all data in your database.  
  Are you sure you want to proceed? (y/N)              

y
>> doctrine  dropping databases
>> doctrine  creating databases
>> doctrine  generating model classes
>> doctrine  generating sql for models
>> doctrine  generating form classes
>> doctrine  generating filter form classes
>> doctrine  created tables successfully
>> doctrine  loading data fixtures from "/Us...ymfony12doctrine/data/fixtures"

上述のコマンドの実行は次のコマンドを個別に実行することと同じです。

$ ./symfony doctrine:drop-db

  This command will remove all data in your database.  
  Are you sure you want to proceed? (y/N)              

y
>> doctrine  dropping databases
$ ./symfony doctrine:build-db
>> doctrine  creating databases
$ ./symfony doctrine:build-model
>> doctrine  generating model classes
$ ./symfony doctrine:build-sql
>> doctrine  generating sql for models
$ ./symfony doctrine:build-form
>> doctrine  generating form classes
$ ./symfony doctrine:build-filters
>> doctrine  generating filter form classes
$ ./symfony doctrine:insert-sql
>> doctrine  created tables successfully
$ ./symfony doctrine:data-load
>> doctrine  loading data fixtures from "/Us...ymfony12doctrine/data/fixtures"

YAMLスキーマファイルから生成されたモデルはlib/model/doctrine and lib/model/doctrine/baseで見ることができます。 ベースディレクトリの中にモデルが存在しないとき、モデルをビルドするたびに生成フォルダの中のファイルは書き換えられます。 lib/model/doctrineの中のクラスを編集することでモデルをカスタマイズできます。

lib/model/doctrine/base/BaseClient.class.phpの内容は次のようになります。

<?php
// Connection Component Binding
Doctrine_Manager::getInstance()->bindComponent('Client', 'master');
 
/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
abstract class BaseClient extends sfDoctrineRecord
{
  public function setTableDefinition()
  {
    $this->setTableName('client');
    $this->hasColumn('name', 'string', 255, array('type' => 'string', 'length' => '255'));
    $this->hasColumn('username', 'string', 255, array('type' => 'string', 'length' => '255'));
    $this->hasColumn('password', 'string', 255, array('type' => 'string', 'length' => '255'));
  }
 
  public function setUp()
  {
    $this->hasMany('Store as Stores', array('local' => 'id',
                                            'foreign' => 'client_id'));
  }
}

開発の最中に./symfony doctrine:build-all-reload-test-allコマンドを走らせることは一般的なやり方です。 このコマンドによって環境全体がリビルドされテストスイートのすべてが実施されます。 これは新しい回帰が起きていないことを保証するために新しいコードをコミットする前に実行する前に使うすばらしいコマンドです。

接続に関する詳細な情報はDoctrineのマニュアルで読むことができます。

3章 - 設定 »
« 1章 - 始める

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.