Orm
Orm 是 物件關聯對映(Object
Relational Mapper) 的简寫,它做兩件事:
對應你資料庫裡的資料列到物件,
并能让你在这些物件之間建立關係。
它緊隨 活动記錄模式(
Active Record Pattern),但也受到其他系统的影響。
建立模型
建立模型花費極少時間,慣例是給类別使用 Model_ 前綴 (例如 Model_Article 使用档案名稱 article.php) 并因此把它們放在 app/classes/model/, 但你可以自由使用任何你選擇的名稱。
class Model_Article extends Orm\Model {}
以上方法只適用於 MySQL 和 MySQLi 的驅动,因为它需要從資料庫中取回模型特性。 然而,这不是很有效率,因此不鼓勵使用这種方式, 因为你始終需要在每个模型多一个額外的查詢就为了取回行名。
class Model_Article extends Orm\Model
{
protected static $_properties = array('id', 'title', 'contents', 'publish');
}
配置
你可以添加靜態特性到該模型來配置它。正如我們看到的,沒有要求但鼓勵設定
$_properties。这些全部可以是 public 和
protected 但可能不是 private。
請注意,所有配置特性都以单一底線前綴,
以預防與你的行名碰撞。
protected static $_table_name
When this isn't set the Model_ prefix is removed from the class name and the class name is pluralized. Thus "Model_Article" expects table "articles". If you don't follow this convention you can change it by setting the $_table_name property.
class Model_Article extends Orm\Model
{
protected static $_table_name = 'myarticles';
}
protected static $_primary_key
預設情況下,这設为 array('id'),如果你使用其他行名稱或多个主鍵, 你需要設定此特性。
class Model_Article extends Orm\Model
{
protected static $_primary_key = array('aid');
}
The primary key must be a real primary key: unique and unchanging. Don't use it for other
purposes (like a foreign key in a one-one relation) as well, that won't work as the PK can't be
changed. The Orm won't check this, and while it might seem to work at first glance: you'll get
into trouble.
It is not required for the PK to be auto_increment (though preferred) and you can specify the PK
yourself, but only the first time. Once it's set, it's set.
protected static $_properties
There's already a simple example above of adding all model properties, they can also be configured by using the column name as the key and setting options like type, label and validation.
class Model_Article extends Orm\Model
{
protected static $_properties = array(
'id', // both validation & typing observers will ignore the PK
'name' => array(
'data_type' => 'varchar',
'label' => 'Article Name',
'validation' => array('required', 'min_length' => array(3), 'max_length' => array(20)),
'form' => array('type' => 'text'),
'default' => 'New article',
),
'gender' => array(
'data_type' => 'varchar',
'label' => 'Gender',
'form' => array('type' => 'select', 'options' => array('m' => 'Male', 'f' => 'Female')),
'validation' => array('required'),
),
'created_at' => array(
'data_type' => 'int',
'label' => 'Created At',
'form' => array(
'type' => false, // this prevents this field from being rendered on a form
),
),
'updated_at' => array('data_type' => 'int', 'label' => 'Updated At')
);
}
Form attributes can be passed in the form of an array as shown in the example.
Validation rules can be passed as either just the rule: array('required') or as the rule with an array of params: array('min_length' => array(3)) both are shown in the example above. A full explanation of the Validation class and it's rule can be found under Core.
protected static $_conditions
By default this property does not exist and Model::condition() returns array(), but you can set on the model if you want any conditions defined on every query run. Currently 'order_by' and 'where' conditions are supported.
class Model_Article extends Orm\Model
{
protected static $_conditions = array(
'order_by' => array('id' => 'desc'),
'where' => array(
array('publish_date', '>', 1370721177),
array('published', '=', 1),
),
);
}
The order_by condition is only applied if no other order by clause is defined. The where conditions are added using and to any other defined where clauses.
protected static $_has_one, $_belongs_to, $_has_many, $_many_many
模型的相互關聯在 關聯模型 裡有解釋。
protected static $_connection
在預設情況下这个特性不存在,且 Model::connection() 回傳 null, 但你可以設定它为任何其他配置在 app/config/db.php 的資料庫名稱。
protected static $_write_connection
If you have a master/slave setup for your database environment, you can use this property to define the connection to use for the write master. If configured, the $_connection property will be used to connect to the read slaves.
By default this property does not exist and Model::connection(true) returns either the configured $_connection, or null, but you can set it to any other database name configured in app/config/db.php.
// 給单一資料庫使用的配置
class Model_Article extends Orm\Model
{
// 'articles_db' 必須被配置在你的 db.php 配置档案
protected static $_connection = 'articles_db';
}
// 給 master/slave 資料庫使用的配置
class Model_Article extends Orm\Model
{
// 'articles_master_db' 和 'articles_slave_db' 必須被配置在你的 db.php 配置档案
protected static $_write_connection = 'articles_master_db';
protected static $_connection = 'articles_slave_db';
}
Note that relations do not work across connections, so when you define your connections in the model, make sure your related models have exactly the same configuration, both for reads and writes!
protected static $_to_array_exclude
If you are writing an API driven application, often you want to limit the columns that are exposed to the API interface. For example, when exposing a user record, you would not want to expose the user's password, login hash or salt.
In your model definition, you can add a list of columns to the to_array_exclude array to exclude them from being exported using to_array().
class Model_User extends Orm\Model
{
protected static $_to_array_exclude = array(
'password', 'login_hash', 'salt' // 排除这些行被導出
);
}
By default the to_array() method exports both column and relation data loaded in the current model object. Optionally, it allows you to export the custom properties too. Array exclusion works on column names, custom properties and relations, allowing you to exclude properties from all of them.
protected static $_observers
添加觀察者在 觀察者 裡有解釋。