ORM是什么
ORM是Object Relational Mapper的简称,他主要做两个事情:1.将数据的每一行映射成一个对象;2.允许你在这些对象之间建立关系;
介绍
不同于其他许多的ActiveRecord,我们实现主要追求精简、快速和易用。它竟可能容易试图让你创建、更新、删除数据库中的信息,处理这些主要是努力为了方便你在开发时候更好的处理数据。
安装
ORM包已经保函在下载的Fuel PHP包中,你只需要在配置中启用即可:
'packages'=>array(
'orm',
),
问题
这里列出一些常见的问题。
My relations/foreign keys aren't being saved (1)
This happens most often when you're using the wrong type of relationship. Especially Has-one and Belongs-to tend to get mixed up. Reread the examples in the docs to make sure you're using the correct relation types and check if everything is configured correctly.
I can't relate objects (2)
Make sure your model is extending Orm\Model and not Model_Crud.
I get an exception that my related model in a package/module can't be found
Make sure the package or module is loaded by Fuel, otherwise the Autoloader won't be able to find the class.
And when configuring the related model "model_to" make sure you configure the full classname, this includes the namespace even if you're in that namespace. Classnames in strings are always taken from global context, no matter the current namespace context.
I get an Orm\FrozenObject exception
Objects can't be edited while being saved to the database, that prevents circular saving and resaving already saved objects. This shouldn't happen during normal usage and is most often caused by faulty configuration of relations.
Sometimes this can be caused by a bug, especially when you're not using a stable release.
创建模型
创建一个模型需要花费一些时间,类名需要采用Model_前缀(举例:我们的文件名为article.php),文件存放在app/classes/model/,当然你可以根据自己需要任意命名。
classModel_ArticleextendsOrm\Model{}
注意:
以上仅适用于MySQL和MySQLi驱动,因为它需要从数据库获取模型属性。但这不是非常高效,因此放弃使用这种方式,因为你总是会需要一个额外的查询每个模型只是为了获取columnnames。
classModel_ArticleextendsOrm\Model
{
protectedstatic$_properties=array('id','title','contents','publish');
}
配置
我们需要配置模型的属性,我们可以使用public或者proteched,但不能使用private。
定义$_table_name
这个就是你需要操作的数据库 名称,如果为空,数据表名称则是你模型前缀后面的单词复数,例如“Model_Article”,那么数据表就是“articles”,如果不是,那我们就需要自定义配置:
classModel_ArticleextendsOrm\Model
{
protectedstatic$_table_name='myarticles';
}
定义$_primary_key
默认是array('id'),如果你的数据表主键不是,则需要重新定义:
classModel_ArticleextendsOrm\Model
{
protectedstatic$_primary_key=array('aid');
}
定义$_properties
这里已经有一个完整的属性例子,你可以参考这样配置,这里保函了字段类型,和校验规则:
classModel_ArticleextendsOrm\Model
{
protectedstatic$_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')
);
}
$_has_one, $_belongs_to, $_has_many, $_many_many, $_many_through属性
CIT原创翻译|http://www.cit.cn/tech/develop/fuelphp/2012/0828/7914.html