请选择 进入手机版 | 继续访问电脑版

在项目中封装万能的增、删、改、查

[复制链接]
查看1302 | 回复0 | 2020-11-24 10:10 | 显示全部楼层 |阅读模式
由于封装的方法可以操作所有的表,可以这些方法封装在基础模型中
  1. <?php
  2. namespace Core;
  3. //基础模型
  4. class Model {
  5.     protected $mypdo;
  6.     private $table; //表名
  7.     private $pk;    //主键
  8.     public function __construct($table='') {
  9.         $this->initMyPDO();
  10.         $this->initTable($table);
  11.         $this->getPrimaryKey();
  12.     }
  13.     //连接数据库
  14.     private function initMyPDO() {
  15.         $this->mypdo= MyPDO::getInstance($GLOBALS['config']['database']);
  16.     }
  17.     //获取表名
  18.     private function initTable($table){
  19.         if($table!='')                //直接给基础模型传递表名
  20.             $this->table=$table;
  21.         else {                                //实例化子类模型
  22.             $this->table=substr(basename(get_class($this)),0,-5);
  23.         }
  24.     }
  25.     //获取主键
  26.     private function getPrimaryKey() {
  27.         $rs=$this->mypdo->fetchAll("desc `{$this->table}`");
  28.         foreach($rs as $rows){
  29.             if($rows['Key']=='PRI'){
  30.                 $this->pk=$rows['Field'];
  31.                 break;
  32.             }
  33.         }
  34.     }
  35.     //万能的插入
  36.     public function insert($data){
  37.         $keys=array_keys($data);                //获取所有的字段名
  38.         $keys=array_map(function($key){        //在所有的字段名上添加反引号
  39.                 return "`{$key}`";
  40.         },$keys);
  41.         $keys=implode(',',$keys);                //字段名用逗号连接起来
  42.         $values=array_values($data);        //获取所有的值
  43.         $values=array_map(function($value){        //所有的值上添加单引号
  44.                 return "'{$value}'";
  45.         },$values);
  46.         $values=implode(',',$values);        //值通过逗号连接起来
  47.         $sql="insert into `{$this->table}` ($keys) values ($values)";
  48.         return $this->mypdo->exec($sql);
  49.     }
  50.     //万能的更新
  51.     public function update($data){
  52.         $keys=array_keys($data);        //获取所有键
  53.         $index=array_search($this->pk,$keys);        //返回主键在数组中的下标
  54.         unset($keys[$index]);                //删除主键
  55.         $keys=array_map(function($key) use ($data){
  56.                 return "`{$key}`='{$data[$key]}'";
  57.         },$keys);
  58.         $keys=implode(',',$keys);
  59.         $sql="update `{$this->table}` set $keys where $this->pk='{$data[$this->pk]}'";
  60.         return $this->mypdo->exec($sql);
  61.     }
  62.     //删除
  63.     public function delete($id){
  64.         $sql="delete from `{$this->table}` where `{$this->pk}`='$id'";
  65.         return $this->mypdo->exec($sql);
  66.     }
  67.     //查询,返回二维数组
  68.     public function select($cond=array()){
  69.         $sql="select * from `{$this->table}` where 1";
  70.         if(!empty($cond)){
  71.             foreach($cond as $k=>$v){
  72.                 if(is_array($v)){        //条件的值是数组类型
  73.                         switch($v[0]){        //$v[0]保存的是符号,$v[1]是值
  74.                                 case 'eq':                //等于  equal
  75.                                         $op='=';
  76.                                         break;
  77.                                 case 'gt':                //大于  greater than
  78.                                         $op='>';
  79.                                         break;
  80.                                 case 'lt':
  81.                                         $op='<';
  82.                                         break;
  83.                                 case 'gte':
  84.                                 case 'egt':
  85.                                         $op='>=';
  86.                                         break;
  87.                                 case 'lte':
  88.                                 case 'elt':
  89.                                         $op='<=';
  90.                                         break;
  91.                                 case 'neq':
  92.                                         $op='<>';
  93.                                         break;
  94.                         }
  95.                         $sql.=" and `$k` $op '$v[1]'";
  96.                 }else{
  97.                         $sql.=" and `$k`='$v'";
  98.                 }
  99.             }
  100.         }
  101.         return $this->mypdo->fetchAll($sql);
  102.     }
  103.     //查询,返回一维数组
  104.     public function find($id){
  105.         $sql="select * from `{$this->table}` where `{$this->pk}`='$id'";
  106.         return $this->mypdo->fetchRow($sql);
  107.     }
  108. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-3-24