Kohana ORM 基本知识和操作

1开启ORM功能
在 bootstrap.php文件修改调用Kohana::modules 模块

Kohana::modules(array(
    ...
    'database' => MODPATH.'database',
    'orm' => MODPATH.'orm',
    ...
));

2创建模型
例如数据库中有一张表teachers
则需要在新建php文件application/classes/model/teacher.php

class Model_Teacher extends ORM
{

   protected $_table_name = 'strange_tablename';   //请注意:【1】如果不重写属性$_table_name,系统会默认Model_Teacher模型保存记录在 teachers数据表中。【2】重写protected $_table_name = 'exampletable'属性,用来指定自定义的数据表名称为exampletable
   protected $_primary_key = 'buy_id';//ORM默认每个model(数据库表)都是以id作为索引和唯一(主键),如果model映射的表主键名不是id,可以通过这个属性设置主键名;
   protected $_db_group = 'alternate';//设置数据库连接,不设置为默认

   protected $_created_column = array(
        'column'    => 'lastupdate',
        'format'    => TRUE,
    );
  protected $_updated_column = array(
        'column'    => 'lastupdate',
        'format'    => TRUE,
    );//时间戳 如果数据表中有需要自动更新时间的字段——如记录数据的插入时间,可以通过$_created_column,而数据变更时间可以通过$_updated_column来自动维护字段值. array中指定了映射表中与之对应的字段名,和时间格式
}

3基本使用

* 创建一个新的用户 
 */  
$user = new Model_User();  //or $user=ORM::factory('user')  
$user->name = 'name';  
$user->password = "password";  
$user->save();  

/** 
 * 查找一个用户 
 */  
$user = ORM::factory('user')->where('id', '=', 10)->find();     
//or $user = ORM::factory('user', 10);  
//or $user = new Model_User(10);  

/** 
 * 删除一个用户 
 */  
$user->delete();  

/** 
 * 检测ORM是否成功加载了一个记录 
 */  
if ($user->loaded()){  
    // Load was successful  
}else{  
    // Error  
}  

/** 
 * 更新一个用户 
 */  
 $user = new Model_User(10);  
 $user->email = "newemail";  
 $user->save();  

/** 
 * 删除一个用户 
 */  
 $user = new Model_User(10);  
 $user->delete();  
转于:http://www.bsdcn.com/ Kohana 中文手册[情人节专版] 本手册为 Kohana Docs v2.2 版本。 本手册制作日期:2009年02月10日 本手册由 icyleaf 制作 --- 参考 常规(General) Kohana 文件系统(Filesystem) - 汉化度 100% 配置(Configuration) - 汉化度 100% URLs - 汉化度 100% 路由(Routing) - 汉化度 99% 加载资源(Loading) - 汉化度 100% 控制器(Controllers) - 汉化度 100% 库(Libraries) - 汉化度 100% 辅助函数(Helpers) - 汉化度 100% 视图(Views) - 汉化度 100% 模型(Models) - 汉化度 100% 事件(Events) - 汉化度 85% 钩子(Hooks) - 汉化度 100% 错误处理(Error Handling) - 汉化度 100% 模块(Modules) - 汉化度 100% 国际化(i18n) - 汉化度 100% 日志(Logging) - 汉化度 100% 核心类(Core) 基准测试类(Benchmark Class) - 汉化度 100% 事件类(Event Class) - 汉化度 100% Kohana 类 - 汉化度 100% Unicode 类 - 汉化度 100% 视图库(View Class) - 汉化度 100% 核心库(Libraries) 缓存库(Cache Library) - 汉化度 100% 日历库(Calendar Library) - 汉化度 95% 验证库(Captcha Library) - 汉化度 99% 数据库库(Database Library) - 汉化度 40% 加密库(Encrypt Library) - 汉化度 100% 图像库(Image Library) - 汉化度 20% 输入库(Input Library) - 汉化度 0% ORM 库 - 汉化度 100% 分页库(Pagination Library) - 汉化度 99% 分析库(Profiler Library) - 汉化度 100% Session 库 - 汉化度 100% URI 库 - 汉化度 99% 校验库(Validation Library) - 汉化度 99% 辅助函数(Helpers) 数组辅助函数 - 汉化度 100% Cookie 辅助函数 - 汉化度 98% 日期辅助函数 - 汉化度 100% 下载辅助函数 - 汉化度 100% Email 辅助函数 - 汉化度 100% Expires Helper - 汉化度 0% Feed 辅助函数 - 汉化度 100% 文件辅助函数 - 汉化度 100% 表单辅助函数 - 汉化度 45% HTML 辅助函数 - 汉化度 100% Inflector Helper - 汉化度 0% 数字辅助函数 - 汉化度 100% 请求辅助函数 - 汉化度 80% 安全性辅助函数 - 汉化度 100% 文本辅助函数 - 汉化度 20% 上传辅助函数 - 汉化度 100% URL 辅助函数 - 汉化度 100% 校验辅助函数 - 汉化度 8% 附加模块(Addons) Archive 扩展 - 汉化度 99% Auth 扩展 - 汉化度 100% Gmaps 扩展 - 汉化度 100% Kodoc 扩展 - 汉化度 100% Payment 扩展 - 汉化度 99%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Casionx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值