The symfony and Doctrine book

5章 - データのフィクスチャ

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

導入

オリジナル

リレーションをリンクする

多対多

インライン

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はYAML構文を使うサンプルのテストデータの小さなセットをロードする機能を提供します。 テーブル用の情報とレコード間のリンクの作成機能がサポートされます。

この章で示される例は次の単純なUserPhonenumberスキーマを使い、これらはconfig/doctrine/schema.ymlに設置されます。

User:
  columns:
    username: string(255)
    password: string(255)

Phonenumber:
  columns:
    user_id: integer
    phonenumber: string(25)
  relations:
    User:
      foreignAlias: Phonenumbers

Profile:
  columns:
    name: string(255)
    about: string(500)
    user_id: integer
  relations:
    User:
      foreignType: one

sfPropelPluginでは外部キーの名前が使われるのは異なり、sfDoctrinePluginでは、データフィクスチャのレコードをリンクするときにリレーションの名前を使います。 データフィクスチャをインラインで指定することもできます。 一方で、Doctrine_Recordインスタンスを表すYAMLブロックはDoctrine_Recordの子のためのリレーショングラフを定義する入れ子のデータ構造を持つことができます。 この章の後でoriginalinlineスタイルの両方のデータフィクスチャを示します。

オリジナル

data/fixtures/user.ymlを作成し次のYAMLコードをロードします。

User:
  User_1:
    username: jwage
    password: changeme
  User_2:
    username: fabpot
    password: changeme
  User_3:
    username: dwhittle
    password: changeme

データベースをリビルドするには次のコマンドを実行します。

$ ./symfony doctrine:build-all-reload

データが適切にロードされたことを検査するために次のシンプルなDQLクエリを実行します。

$ ./symfony doctrine:dql "FROM User u"
>> doctrine  executing dql query
DQL: FROM User u
found 3 results
-
  id: '1'
  username: jwage
  password: changeme
-
  id: '2'
  username: fabpot
  password: changeme
-
  id: '3'
  username: dwhittle
  password: changeme

データフィクスチャでデータの値を設定する

sfYamlパーサーに関して文字列の型を強制するシングルクォートで明示的にラップしない限り、sfYamlパーサーは自動的に有効なデータをUnixタイムスタンプに変換します。 dateもしくはtimestampカラム型を設定するときにシングルクォートを使わないのであればDoctrineのバリデーションは失敗します。 Doctrine_Recordに渡される値がUnixタイムスタンプだからです。

Userモデルのcreated_atカラムを設定する方法は次のとおりです。

User:
  User_1:
    username: jwage
    password: changeme
    created_at: '2008-12-17 00:01:00'

リレーションをリンクする

data/fixtures/phonenumber.ymlを作成し次のYAMLデータフィクスチャをロードします。

Phonenumber:
  Phonenumber_1:
    phonenumber: 6155139185
    User: User_1
  Phonenumber_2:
    phonenumber: 1234567890
    User: User_2
  Phonenumber_3:
    phonenumber: 0987654321
    User: User_3

データベースをリビルドしてロードされたデータフィクスチャを検査する別のDQLクエリを実行します。

$ ./symfony doctrine:build-all-reload

ではUserPhonenumberレコードをJOINするより複雑なクエリでデータを検査します。

$ ./symfony doctrine:dql "FROM User u, u.Phonenumbers p"
>> doctrine  executing dql query
DQL: FROM User u, u.Phonenumbers p
found 3 results
-
  id: '1'
  username: jwage
  password: changeme
  Phonenumbers:
    -
      id: '1'
      phonenumber: 6155139185
      user_id: '1'
-
  id: '2'
  username: fabpot
  password: changeme
  Phonenumbers:
    -
      id: '2'
      phonenumber: 1234567890
      user_id: '2'
-
  id: '3'
  username: dwhittle
  password: changeme
  Phonenumbers:
    -
      id: '3'
      phonenumber: 0987654321
      user_id: '3'

多対多

次のデータフィクスチャでconfig/doctrine/schema.ymlのYAMLスキーマを使います。

BlogPost:
  columns:
    title: string(255)
    body: clob
  relations:
    Tags:
      class: Tag
      refClass: BlogPostTag
      foreignAlias: BlogPosts

BlogPostTag:
  columns:
    blog_post_id:
      type: integer
      primary: true
    tag_id:
      type: integer
      primary: true
  relations:
    BlogPost:
      foreignAlias: BlogPostTags
    Tag:
      foreignAlias: BlogPostTags

Tag:
  columns:
    name: string(255)

データフィクスチャのdata/fixtures/data.ymlをロードします。

BlogPost:
  BlogPost_1:
    title: Test Blog Post
    body: This is the body of the test blog post
    Tags: [test, php, doctrine, orm]

Tag:
  test:
    name: test
  php:
    name: php
  doctrine:
    name: doctrine
  orm:
    name: orm

データベースをリビルドしてロードされたデータを見るためにDQLクエリを実行します。

$ ./symfony doctrine:build-all-reload

BlogPostレコードと関連Tagsのすべてを取得する別のDQLクエリでデータを検査します。

$ ./symfony doctrine:dql "FROM BlogPost p, p.Tags"
>> doctrine  executing dql query
DQL: FROM BlogPost p, p.Tags
found 1 results
-
  id: '1'
  title: 'Test Blog Post'
  body: 'This is the body of the test blog post'
  Tags:
    -
      id: '1'
      name: test
    -
      id: '2'
      name: php
    -
      id: '3'
      name: doctrine
    -
      id: '4'
      name: orm

インライン

Doctrineはインラインでのデータフィクスチャのリレーションを指定する機能を提供します。

User:
  User_1:
    username: jwage
    password: changeme
    Phonenumbers:
      Phonenumber_1:
        6155139185

BlogPost:
  BlogPost_1:
    title: Test Blog Post
    body: This is the body of the test blog post
    Tags:
      test:
        name: test
      php:
        name: php
      doctrine:
        name: doctrine
      orm:
        name: orm

この代替構文はデータフィクスチャの長さと複雑性を大いに減らすことができます。

データフィクスチャに関する詳細な情報はDoctrineのマニュアルで読むことができます。

6章 - データを扱う »
« 4章 - スキーマファイル

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.