The symfony and Doctrine book

Chương 5 - Data Fixture

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

Giới thiệu

Original

Liên kết các quan hệ

Quan hệ nhiều-nhiều

Inline

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 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.

Giới thiệu

Doctrine hỗ trợ khả năng nạp dữ liệu bằng cách sử dụng cấu trúc YAML đơn giản. Nó giúp bạn dễ dàng tạo dữ liệu cho các bảng và liên kết khóa ngoài giữa chúng.

Ví dụ trong chương này sử dụng schema UserPhonenumber được xác định trong file 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

Trong sfDoctrinePlugin, các bản ghi liên kết với nhau thông qua tên quan hệ, còn sfPropelPlugin sử dụng tên khóa ngoài. Bạn cũng có thể tạo dữ liệu inline. Có nghĩa là, một khối YAML diễn tả một Doctrine_Record có thể chứa dữ liệu của các Doctrine_Record con. Sau đâu chúng ta sẽ xem xét cả 2 cách originalinline.

Original

Tạo file data/fixtures/user.yml và thêm đoạn code YAML.

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

Chạy lệnh sau để tạo lại database.

$ ./symfony doctrine:build-all-reload

Bây giờ thực thi câu truy vấn DQL để lấy dữ liệu vừa nạp vào.

$ ./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

Thiết lập giá trị ngày tháng trong Data Fixtures

sfYaml parser tự động chuyển giá trị ngày tháng sang unix timestamps trừ khi bạn đưa chúng vào trong dấu nháy đơn để parser coi đó là kiểu string. Nếu bạn không sử dụng dấu nháy đơn với các cột có kiểu dữ liệu date hoặc timestamp, Doctrine validation sẽ fail do dữ liệu pass phải là kiểu unix timestamp.

Dưới đây là ví dụ cách thiết lập giá trị cho cột created_at của model User.

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

Liên kết các quan hệ

Tạo file data/fixtures/phonenumber.yml và thêm dữ liệu.

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

Tạo lại database.

$ ./symfony doctrine:build-all-reload

Bây giờ lấy dữ liệu với câu truy vấn thực hiện việc join UserPhonenumber.

$ ./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'

Quan hệ nhiều-nhiều

Sử dụng file YAML schema config/doctrine/schema.yml sau.

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)

Thêm dữ liệu vào file 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

Tạo lại database.

$ ./symfony doctrine:build-all-reload

Bây giờ thực hiện câu truy vấn DQL để lấy tất cả các bản ghi BlogPostTags liên quan

$ ./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

Inline

Doctrine hỗ trợ khả năng mô tả dữ liệu quan hệ inline như dưới đây.

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

Cấu trúc trên giúp giảm độ dài và sự phức tạp của data fixtures.

Bạn có thể tìm hiểu thêm về data fixtures trong Doctrine Manual ở đây.

Chương 6 - Làm việc với dữ liệu »
« Chương 4 - File Schema

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.