The symfony and Doctrine book

Chương 3 - Cấu hình

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

Attribute

Toàn cục

Tất cả kết nối

Model

Cấu hình quá trình tạo Model

Chỉnh sửa đường dẫn tới thư viện Doctrine

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.

Attribute

Doctrine được cấu hình thông qua attribute. Các attribute có thể được thiết lập ở các mức khác nhau và thừa kế lẫn nhau. Một số attribute có thể được thiết lập ở tất cả các mức, một số thì không. Dưới đây miêu tả cách thiết lập các attribute ở mỗi mức. Attribute có thể xác định ở toàn cục, cho từng connection, hay mỗi model riêng biệt.

Trong symfony, cấu hình Doctrine được lưu ở config/ProjectConfiguration.class.php hoặc apps/appname/config/appnameConfiguration.class.php

Toàn cục

Bạn có thể thiết lập global attribute bằng cách tạo phương thức configureDoctrine() trong file cấu hình. Tất cả các global attribute được thiết lập với singleton instance của Doctrine_Manager . Phương thức này được gọi khi sfDoctrinePlugin config.php được load. Ở thời điểm này chưa có bất kì kết nối nào tồn tại, do đó attribute được quản lý bởi Doctrine_Manager.

public function configureDoctrine(Doctrine_Manager $manager)
{
  $manager->setAttribute('use_dql_callbacks', true);
  $manager->setAttribute('use_native_enum', true);
}

Tất cả kết nối

Bạn có thể quản lý các attribute của từng kết nối bằng cách tạo phương thức configureDoctrineConnection() trong lớp cấu hình. Phương thức này được gọi trong sfDoctrineDatabase khi mỗi kết nối được symfony tạo theo thứ tự trong file config/databases.yml.

public function configureDoctrineConnection(Doctrine_Connection $connection)
{
  $connection->setAttribute('use_dql_callbacks', true);
  $connection->setAttribute('use_native_enum', true);
}

Bạn có thể xác định các attribute cho kết nối trực tiếp trong file cấu hình config/doctrine/databases.yml:

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

Bạn cũng có thể cấu hình khác nhau cho mỗi kết nối bằng cách tạo một phương thức ứng với mỗi kết nối. Nếu bạn có một kết nối tên master thì bạn cần tạo một function tên configureDoctrineConnectionMaster() trong file config/ProjectConfiguration.class.php.

public function configureDoctrineConnectionMaster(Doctrine_Connection $connection)
{
  $connection->setAttribute('use_dql_callbacks', false);
  $connection->setAttribute('use_native_enum', false);
}

Trong ví dụ trên chúng ta đã thiết lập use_dql_callbacksuse_native_enum cho tất cả các kết nối ngoại trừ kết nối có tên master.

Model

Mức cấu hình cuối cùng của Doctrine là model. Attribute có thể được xác định trực tiếp trong file YAML mô tả model.

Store:
  connection: client
  attributes:
    export: tables
  columns:
    name: string(255)
    description: string(500)

Bạn cũng có thể thiết lập các attribute trong các lớp được tạo ra ở thư mục lib/model/doctrine. Mở file lib/model/doctrine/Store.class.php và override phương thức setTableDefinition() để thêm attribute.

public function setTableDefinition()
{
  parent::setTableDefinition();
  $this->setAttribute('export', 'tables');
}

Cấu hình quá trình tạo Model

sfDoctrinePlugin cung cấp khả năng override một số lựa chọn để tạo model. Những thiết lập này có thể điều khiển bằng lớp sfConfig sử dụng tham số doctrine_model_builder_options.

Dưới đây là ví dụ cách đổi tên lớp base khi tạo models. Bạn có thể thiết lập để đổi nó thành myDoctrineRecord hay bất cứ gì bạn muốn.

public function configureDoctrine(Doctrine_Manager $manager)
{
  $options = array('baseClassName' => 'myDoctrineRecord');
  sfConfig::set('doctrine_model_builder_options', $options);
}

Bạn cần tạo thêm một lớp, ở đây là sfproject/lib/myDoctrineRecord.class.php với đoạn code sau.

class myDoctrineRecord extends sfDoctrineRecord
{
}

Bây giờ hãy generate model, tất cả các lớp sẽ extend từ myDoctrineRecord thay vì sfDoctrineRecord, do đó bạn có thể thêm các chỉnh sửa cho tất cả các model của mình.

Dưới đây là danh sách tất cả các option bạn có thể thay đổi thành giá trị khác trong quá trình tạo model.

Tên Mô tả Mặc định
suffix Hậu tố dùng cho các lớp được tạo ra .class.php
generateBaseClasses Có tạo các lớp base hay không true
generateTableClasses Có tạo các lớp *Table hay không true
baseClassPrefix Tiền tố cho các lớp base Base
baseClassesDirectory Thư mục chứa các lớp base base
baseClassName Lớp cha của các model sfDoctrineRecord

Chỉnh sửa đường dẫn tới thư viện Doctrine

Với sfDoctrinePlugin thật dễ dàng để thêm một phiên bản mới của Doctrine bằng thay đổi giá trị cấu hình.

Dưới đây là ví dụ về cách cấu hình sfDoctrinePlugin để sử dụng một phiên bản khác của Doctrine, ví dụ 1.1.

Đầu tiên chúng ta cần thêm phiên bản của Doctrine chúng ta muốn sử dụng vào thư mục lib/vendor/doctrine:

$ mkdir lib/vendor
$ cd lib/vendor
$ svn co http://svn.doctrine-project.org/branches/1.0.10/lib doctrine

Bây giờ chúng ta có thể cấu hình sfDoctrinePlugin để sử dụng phiên bản này thay vì phiên bản có sẵn trong plugin. Trong phương thức ProjectConfiguration::setup() bạn cần thay đổi giá trị của sfDoctrinePlugin_doctrine_lib_path như dưới đây:

public function setup()
{
  sfConfig::set('sfDoctrinePlugin_doctrine_lib_path', sfConfig::get('sf_lib_dir') . '/vendor/doctrine/Doctrine.php');
}

Bạn có thể tìm hiểu thêm về cấu hình ở Doctrine Manual.

Chương 4 - File Schema »
« Chương 2 - Kết nối

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.