CodeIgniter

codeigniter下ORM的扩展

2015-12-08  本文已影响957人  liangxifeng833

一,什么是ORM?

相信大家都知道,codeigniter是一个轻量级的框架,并不支持ORM,那么以下是本人根据自己的实际项目,在codeigniter上做的ORM扩展.

二,在ci本身连接数据库基础上扩展

$this->db->from('wiki_key')->where(array('id'=>$wikiKeyId))->order_by('id','desc')->limit(1)->initAttr(array(1));                          
$rowObj = $this->db->getObj();
//保存对象
$rowObj->name='zhangsan';
$res = $this->db->save($rowObj);
//销毁对象
$res = $this->db->destory($rowObj);
 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');    
//liangxifeng 2015-06-05  添加ORM控制类
$params['dbdriver_tmp'] = $params['dbdriver'];
if($params['dbdriver']=="mysql")
   {  
       require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/curd_driver.php');
        $params['dbdriver_tmp'] = "curd";
    }   
    // Instantiate the DB adapter
    $driver = 'CI_DB_'.$params['dbdriver_tmp'].'_driver';
    $DB = new $driver($params);

具体实现图解如下:

ci-orm-classci-orm-class
ci-orm-squenceci-orm-squence

三,在ci基于REST的三层架构中的BLL层扩展

//查询多条数据
$inParamList = array('data'=>array('where'=>array('batch_id >= '=>1), 'order'=>'batch_id asc','limit'=>10));
$inParamList['type'] = 'list';
$batchArrayObj = $this->rest_client->get('ticket/ticket_batchs',$inParamList,1);
//查询单条数据
$batchRowObj = $this->rest_client->get('ticket/ticket_batch',array('id'=>1,'field'=>'batch_id,batch_price,batch_name'),1);
//在原有查询数据对象基础上save
$batchRowObj->batch_name='lisi';
$this->batchRowObj->save();
//或new空对象进行save
$this->load->library('ticket/ticket_batch',array(),'batchObj');
$this->batchObj->batch_name = 'test2';
$this->batchObj->save();
//销毁对象
$this->batchObj->destroy();

具体实现图解如下:

ci-orm-bll-classci-orm-bll-class
ci-orm-bll-squenceci-orm-bll-squence
上一篇下一篇

猜你喜欢

热点阅读