The symfony and Doctrine book

Chương 7 - Migration

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

Các task với Migration

Schema & Data Fixtures

Tạo Migration

Từ Database

Từ Model

Skeleton

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.

Một vấn đề thường gặp trong phát triển web là quản lý sự thay đổi database khi thay đổi model và schema. Migration trong Doctrine cung cấp một giải pháp hiệu quả để giải quyết vấn đề này.

sfDoctrinePlugin cung cấp thêm một số task để tự động tạo các lớp migration từ cơ sở dữ liệu có sẵn hoặc model. Bạn cũng có thể sử dụng Doctrine để tạo lớp migration của mình.

Các task với Migration

:generate-migration          Tạo lớp migration (doctrine-generate-migration)
:generate-migrations-db      Tạo lớp migration từ cơ sở dữ liệu (doctrine-generate-migrations-db, doctrine-gen-migrations-from-db)
:generate-migrations-models  Tạo lớp migration từ các model (doctrine-generate-migrations-models, doctrine-gen-migrations-from-models)
:migrate                     Chuyển database tới phiên bản hiện tại, hoặc phiên bản nào đó (doctrine-migrate)

Các ví dụ trong chương này giả định rằng bạn đang làm việc với schema và data fixture dưới đây. Chúng ta sẽ sử dụng chúng làm cơ sở cho tất cả các ví dụ trong chương này.

Schema & Data Fixtures

project/config/doctrine/schema.yml

BlogPost:
  actAs:
    I18n:
      fields: [title, body]
    Timestampable:
  columns:
    author: string(255)
    title: string(255)
    body: clob
  relations:
    Tags:
      class: Tag
      refClass: BlogPostTag
      foreignAlias: BlogPosts
Tag:
  columns:
    name: string(255)
BlogPostTag:
  columns:
    blog_post_id:
      type: integer
      primary: true
    tag_id:
      type: integer
      primary: true

project/data/fixtures.yml

BlogPost:
  BlogPost_1:
    author: Jonathan H. Wage
    Translation:
      en:
        title: Test Blog Post
        body: This is the body of the test blog post
    Tags: [php, orm]
Tag:
  php:
    name: PHP
  orm:
    name: ORM

Tạo Migration

Doctrine cung cấp khả năng tạo lớp migration từ cơ sở dữ liệu hoặc model có sẵn, cũng như tạo các migration trống để bạn thêm code vào.

Từ Database

Nếu bạn có sẵn một database bạn có thể tạo lớp migration để có thể tạo lại database cho bạn sau này bằng cách chạy lệnh sau.

$ ./symfony doctrine:generate-migrations-db frontend
>> doctrine  Generated migration classes successfully from database

Từ Model

Nếu bạn có sẵn một tập các model bạn có thể tạo các lớp migration để có thể tạo lại database cho bạn sau này bằng cách chạy lệnh sau.

$ ./symfony doctrine:generate-migrations-models frontend
>> doctrine  Generated migration classes successfully from models

Bây giờ bạn đã có các lớp migration bạn có thể xóa database và chạy lệnh migrate để tạo lại database.

$ ./symfony doctrine:drop-db frontend && ./symfony doctrine:build-db frontend && ./symfony doctrine:migrate frontend && ./symfony doctrine:data-load frontend

Skeleton

Bây giờ database đã được tạo và là phiên bản mới nhất, bạn có thể tạo lớp migration skeleton mới để chuyển schema ứng với sự thay đổi của model. Giả sử rằng bạn cần thêm cột excerpt vào model BlogPost. Dưới đây là BlogPost schema đã được update chứa cột mới.

BlogPost:
  actAs:
    I18n:
      fields: [title, body]
    Timestampable:
  columns:
    author: string(255)
    title: string(255)
    body: clob
    excerpt: string(255)
  relations:
    Tags:
      class: Tag
      refClass: BlogPostTag
      foreignAlias: BlogPosts

Bây giờ chúng ta tạo migration skeleton trống để thêm cột excerpt vào database. Chạy lệnh dưới đây để tạo lớp migration.

$ ./symfony doctrine:generate-migration frontend AddBlogPostExcerptColumn
>> doctrine  Generated migration class: AddB...ymfony12/lib/migration/doctrine

Bây giờ trong thư mục project/lib/migration/doctrine bạn sẽ thấy file mới có tên 006_add_blog_post_excerpt_column.class.php với đoạn code sau.

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class AddBlogPostExcerptColumn extends Doctrine_Migration
{
    public function up()
    {
 
    }
 
    public function down()
    {
 
    }
}

Mỗi migration gồm có phương thức up() và down(). Trong phương thức up() bạn có thể thêm cột, tạo bảng, ... và phương thức down() đơn giản là làm ngược lại những gì trong phương thức up(). Mỗi lớp mô tả một phiên bản của database và các phương thức up() và down() cho phép bạn chuyển qua lại giữa các phiên bản của database.

Bây giờ thêm đoạn code sau vào lớp migration để thêm cột excerpt.

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class AddBlogPostExcerptColumn extends Doctrine_Migration
{
    public function up()
    {
      $this->addColumn('blog_post', 'excerpt', 'string', array('length' => '255'));
    }
 
    public function down()
    {
      $this->removeColumn('blog_post', 'excerpt');
    }
}

Bây giờ chạy lệnh sau để cập nhật database tới phiên bản mới nhất sử dụng lớp migration chúng ta đã viết ở trên để thêm cột excerpt.

$ ./symfony doctrine:migrate frontend
>> doctrine  migrated successfully to version #6

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

Người dịch: Nguyễn Hữu Quân - huu2uan [at] gmail.com.

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

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.