php ci 2.0框架 ORM

   很早知道ci出了2.0版本了。这几天正好有项目要用ci开发 虽然项目不大。不过也从开发项目的过程中熟悉了CI框架

   因为是个电商项目 本来想用个YII2 的。 封装的虽然厉害不过功能强大 因为另个兄弟坚持所以采用了CI 开搞前特地问了下新版有没ORM(我知道老版没有- -)

   被告知有了ORM 结果开发起来发现没有。。 于是自己简单实现了个 不吐槽了 直接上代码吧。。。    

 

<?php
class Orm_Model extends  MY_Model{
   protected $pk = 'id';
   protected $_isNew = false;
   protected $_ID = null; 
   protected $_tableName;
   protected $_arRelationMap;
   protected $_DB;
   protected $_attrs = array();

   public function __consturct(){
        parent::__construct();   
       $this->_tableName = $this->getTableName();
       $this->_arRelationMap = $this->getRelationMap();
   }
   
   protected function getTableName(){}
   protected function getRelationMap(){}
   
   public function getAttr(){
           return $this->_attrs;
   }
   
   public function find( $condition ){
           $keys = implode( '`,`', array_keys( $this->getRelationMap() ));
        $ret = $this->db_r->select($keys)->where( $condition )->limit(1)->get( $this->getTableName() );
        $this->_attrs =  $ret->row_array();
        if( empty( $this->_attrs[$this->pk] ) )
            $this->_isNew = true;
        return $this;
   }
   
   public function save(){
           $pKey = (isset( $this->_attrs[$this->pk] ) && !empty( $this->_attrs[$this->pk] )) ? $this->_attrs[$this->pk] : '';
           if( empty(  $pKey ) || $this->_isNew ){
            return $this->db_w->insert( $this->getTableName() , $this->_attrs );
           }else{
            $this->db_w->where( $this->pk , $pKey );
            return $this->db_w->update( $this->getTableName(), $this->_attrs ); 
           }
   }
   
   public function __call($method,$param){
      $type   = substr($method,0,3);
      $member = substr($method,4);
      switch($type){
         case 'get':
             return $this->getMember($member);
             break;
         case 'set':
             return $this->setMember($member,$param[0]);
      }
      return false;
   }
   
   public function setMember($key , $val){
        if( !$this->checkAttr($key) )
               return false;
           $this->_attrs[$key] = $val;
   }
   
   public function getMember($key){
        $ret = null;
           if( $this->checkAttr($key) )
               $ret =   isset( $this->_attrs[$key] ) ? $this->_attrs[$key] : '';
           return $ret;
   }
   
   private function checkAttr( $key ){
           $ret = true;
           $rel = $this->getRelationMap(); 
           if( !array_key_exists( $key, $rel ) )
               $ret =  false;
           return $ret;
   }

   public function del(){
         $pKey = isset( $this->_attrs[$this->pk] ) ? $this->_attrs[$this->pk] : '';
      if( !empty( $pKey ) ){
         return  $this->db_w->delete( $this->getTableName() , array($this->pk =>$pKey)); 
      }
      return false;
   }
}
?>

使用:

model:

<?php
class User_model extends Orm_Model
{
    protected $pk = 'id';
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getRelationMap(){
        return array(
             'id'       => 'ID',
             'user_name'=> 'USER_ID',
             'user_passwd' => 'CART_INFO',
             'email' => 'TOTAL_PRICE',
             'mobile' => 'UPDATE_TIME',
             'point' => 'ADD_TIME',
             'last_login_ip' => 'STATUS',
             'add_time' => 'STATUS'
        );
    }
    
     public function getTableName(){
         return 'user';
     }
        
}
<?php
/**
 * 
 * Enter description here ...
 * @author Administrator
 *
 */
class User_service {

    function __construct(){
        $this->Obj = &get_instance();
        $this->Obj->load->model('user_model');
        $this->user_model = $this->Obj->user_model;
    }
    
    
    public function login( $params ){
        $this->user_model->find(array('user_name'=> $params['user_name'] ) );
          // xxxxxx
       }
}    

 

posted @ 2015-04-30 23:41  ﹏Sakura  阅读(905)  评论(0编辑  收藏  举报