PEAR DB_DataObject 简介

进阶

配置选项

共有三个ini文件存放各种信息,

DataObject.ini (或config.ini)名字由用户自己定义。设置数据库、路径等信息
db.ini 执行CreateDataObjectClasses.php自动创建,保存数据库各字段的类型
db.links.ini 保存表间关系,不同表通过什么字段来联系。

其中db用你的数据库名称替换。

DataObject.ini
信息放在[DB_DataObject]段内

 [DB_DataObject]
 database = mysql://user:password@server/database
 schema_location = /DataObjects
 class_location  = /DataObjects
 require_prefix  = /DataObjects/

database:数据库访问信息,与DB的格式一样

schema_location:存放表间关系的ini文件

class_location:自动生成的DataObject类放置的路径

require_prefix:派生类放置的路径,最后为“/”。与include路径的相对路径。 staticGet()和getLinks()方法自动载入类时要搜索到相应的类。

一个linux下的例子:
example.ini:

[DB_DataObject]database    = mysql://user:password@localhost/vending
schema_location = /home/me/Projects/myapplication/DataObject
sclass_location  = /home/me/Projects/myapplication/DataObject
srequire_prefix  = DataObjects/
class_prefix    = DataObjects_
$config = parse_ini_file('example.ini',TRUE);
foreach($config as $class=>$values)
 {
    $options = &PEAR::getStaticProperty($class,'options');
    $options = $values;
}
// 也可以不使用ini文件的方式,而使用$options组件:
$options = &PEAR::getStaticProperty('DB_DataObject','options');
$options = array(
    'database'         => 'mysql://user:password@localhost/vending',
    'schema_location'  => '/home/me/Projects/myapplication/DataObjects',
    'class_location'   => '/home/me/Projects/myapplication/DataObjects',
    'require_prefix'   => 'DataObjects/',
    'class_prefix'     => 'DataObjects_',);

上述四个是必先项,以下为可选项:

sequence_{table} string:强行设置次序键(autoincrement/nextval类型)。当主键不能被正确识别而运行不正确,可以强行设置次序键

例如:sequence_person = login 将person表的次序键设为login字段。

ignore_sequence_keys string 忽略顺序键

If you do not want to use pear’s nextval(), for automatically filling in sequences, this can disable it for “ALL”, or a list of tables “person,cart,group”

debug integer 调试级别。这个选项非常有用,调试时可以看到运行的情况。不过我一般会用->debug() 方法在一个公共文件中设置。默认为关闭 (default 0=off), 1= basic sql logging,2=result logging, 3=everything

debug_ignore_updates boolean default FALSE, if set, then updates on the database are disabled.

dont_die boolean default FALSE, The standard behaviour of dataobjects is to issue a PEAR_ERROR_DIE (eg. exiting PHP), when a fatal error occurs, like database connection failure or sending an invalid object type to a method. However if you need to run it on a live server you will probably want to set this to TRUE and define a PEAR error handler to catch these errors and show a nice friendly ‘sorry we are down for maintenence’ message page.

Configuration Options – Multiple Databases (optional)

database_* string When you have multiple databases you can use the database_* to specify the DSN for each database

Example 20-4. using multiple databases – database passwords

database_authentication = mysql://user:password@localhost/authentication
 database_sales = mysql://user:password@localhost/sales

table_* string When you have multiple databases you can use the table_* configuration variables to map individual tables to different databases, for example

Example 20-5. using multiple databases – table settings

table_users = authentication
 table_saleslog = sales
 table_stock = sales
Configuration Options – Builder

class_location directory The Directory where your DataObject extended Classes are.

Used by the Class Auto Builder when updating/writing to your class definitions.

extends string The Name of your Base Class (usually DB_DataObject) is located.

If you wish to add a common layer of useful methods for all classes, you can set the extends_location and extends settings to a different class. the default is ‘DB_DataObject’

extends_location directory The Directory where your Base Class (usually DB_DataObject) is located.

If you wish to add a common layer of useful methods for all classes, you can set the extends_location and extends settings to a different class. the default is ‘DB/DataObject.php’

generator_class_rewrite directory

定义表间关系 db.links.ini

结构

表间关系
1:1(一对一关系)

n:1(一对多关系,或者是多对一关系)通常表示一个对象的属性也是一个对象。

n:m(多对多关系)使用一个十字链表(crossLink table)表示它们之间的关系。

对数据表的要求

每个表的主键(primaryKey)必须是一个以唯一数字标识的字段(sequenceKey),在mysql中就是auto_increment的字段。不可以使用非整型的字段作为主键,即使是设为unique;也不可以用多字段做主键。这与数据库设计的理论有些不同。例如以username作为主键,会出现找不到键的错误。虽然可以用 keys()方法强行设置主键,但我建议不要这样做。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.