辛苦了,顶一个,save分开了是感觉好很多,不过有时也觉得麻烦,要多写一句代码了这叫不知足,嘎嘎。
KO3: ORM 3.1 将会重构,修改和添加新特性
ORM will be somewhat re-structured in 3.1.0. My goal is to fix some of the inherent design flaws as well as increase performance and usability.
以上原文来自官方论坛。
ORM 3.1 源码: http://github.com/kohana/orm/blob/3.1.0/classes/kohana/orm.php
更改历史:
* 移除惰性加载
* 移除 save() 方法
* 添加 create() 和 update() 方法
* 更新 has_many 访问方法(无须 find_all 查找)
* 优化代码
=========
详情
=========
1. has_many 更改
在 3.1.0 之前的版本,has_many 关系需要这样访问使用:
$user->accounts->find_all()
现在无须这样费功夫了 - has_many 关系将会在 3.1.0 中得到改善:
$user->accounts
如果你相修改查询,你只需要在:
$user->where('active', '=', 1)->accounts
2. create() 和 update() 方法
之前的 save() 方法已经被 create() 和 update() 方法代替。这将会减轻很多代码混乱和恶梦。
创建一条记录:
// 自动选择主键
$user = ORM::factory('user');
$user->name = 'John';
$user->create();
// 指定主键
$user = ORM::factory('user');
$user->id = 5;
$user->create();
// 一行完成创建(cool!)
ORM::factory('user')->set('name', 'John')->create();
更新记录
// 加载用户 ID 5 并更改名称为 'Bob'
$user = ORM::factory('user', 5);
$user->name = 'Bob';
$user->update();
// 更改 Bob 的主键为 6
$user->id = 6;
$user->update();
// 在为提前加载的情况下更新记录 ID 5(尽用于一个数据库的调用)
$user = ORM::factory('user')->set('name', 'Bob')->update(5);
3. 多记录更新和删除
你可以更新多记录(或删除它们)通过给 update() 和 delete() 方法传递一个 TRUE 参数:
// 删除所有未激活记录
ORM::factory('user')->where('active', '=', 0)->delete(TRUE);
// 更改所有激活用户为未激活
ORM::factory('user')->where('active', '=', 1)->set('active', 0)->update(TRUE);
辛苦了,顶一个,save分开了是感觉好很多,不过有时也觉得麻烦,要多写一句代码了这叫不知足,嘎嘎。
发表讨论