![]() |
|
The symfony and Doctrine book5章 - データのフィクスチャ |
|
You are currently reading "The symfony and Doctrine book" which is licensed under the GFDL license.

オブジェクトのリレーションの階層にロードされるデータを指定するためにDoctrineはYAML構文を使うサンプルのテストデータの小さなセットをロードする機能を提供します。 テーブル用の情報とレコード間のリンクの作成機能がサポートされます。
この章で示される例は次の単純な
UserとPhonenumberスキーマを使い、これらは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の子のためのリレーショングラフを定義する入れ子のデータ構造を持つことができます。 この章の後でoriginalとinlineスタイルの両方のデータフィクスチャを示します。
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
ではUserとPhonenumberレコードを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のマニュアルで読むことができます。
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.