• 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
           }
    }    
  • 相关阅读:
    hdu 5795 A Simple Nim 博弈sg函数
    hdu 5724 Chess 博弈sg+状态压缩
    hdu 3094 A tree game 树上sg
    2017"百度之星"程序设计大赛
    hdu 6134 Battlestation Operational 莫比乌斯反演
    HDU 6143 Killer Names DP+快速密
    HDU 6107 Typesetting 倍增
    HDU 6096 String 排序 + 线段树 + 扫描线
    HDU 6086 Rikka with String AC自动机 + DP
    HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/4470129.html
Copyright © 2020-2023  润新知