当前位置: 首页 > news >正文

PHP自己的框架PDO数据表前缀、alias、model、table、join方法实现(完善篇九--结束)

一、实现功能,数据表前缀、alias、model、table、join方法实现

 

 二、表前缀实现

1、config.php增加表前缀
 'DB_PEX'=>'fa_',//数据库前缀
 2、增加表前缀方法function.php
function model($table){$model=new  ModelBase($table,config("DB_PEX"));return $model;
}
function table($table){return  new  ModelBase($table);
}
3、PDO数据表增加表前缀ModelBase.php
    private $pex="";//表前缀public function __construct($table=null,$pex=""){$this->pex=$pex;if($table){$this->table=$this->pex.$table;}if(!$this->table){die("no table" );}$this->_connect();$this->_opt();}

三、alias数据表别名和join实现实现ModelBase.php

    private function _opt(){$this->opt=array('filed'=>'*','where'=>'','group'=>'','having'=>'','order'=>'','limit'=>'','alias'=>'','join'=>'',);}public function alias($as){$this->opt['alias']=  ' as '.$as." ";return $this;}public function join($join,$condition,$type=''){$this->opt['join']=  " {$type} join ".$this->pex.trim($join)." on {$condition}";return $this;}public function select(){$sql = "SELECT ".$this->opt['filed']. " FROM ".$this->table.$this->opt['alias'].$this->opt['join'].$this->opt['where'].$this->opt['group'].$this->opt['having'].$this->opt['order'].$this->opt['limit'];$stmt = $this->pdo->prepare($sql);$stmt->execute();return $stmt->fetchAll(PDO::FETCH_ASSOC);}

四、控制器实现,数据表前缀、alias、model、table、join方法查询

 public function index(){$data= model("test")->filed('t.*,r.role')->alias('t')->join('role r','t.id=r.testId','left')->select();var_dump($data);$data= table("fa_test")->select();var_dump($data);}

五、完整ModelBase.php代码

<?php
class ModelBase
{public $pdo = NULL;public $table = NULL;public $opt;private $pex="";//表前缀public function __construct($table=null,$pex=""){$this->pex=$pex;if($table){$this->table=$this->pex.$table;}if(!$this->table){die("no table" );}$this->_connect();$this->_opt();}private function _connect(){if($this->pdo){return true;}$host = config('DB_HOST');$db = config('DB_DATABASE');$user = config('DB_USER');$pass =config('DB_PWD');$dsn = "mysql:host=$host;dbname=$db;charset=utf8";try {$this->pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);} catch (PDOException $e) {die("数据库连接失败: " . $e->getMessage());}}private function _opt(){$this->opt=array('filed'=>'*','where'=>'','group'=>'','having'=>'','order'=>'','limit'=>'','alias'=>'','join'=>'',);}public function alias($as){$this->opt['alias']=  ' as '.$as." ";return $this;}public function join($join,$condition,$type=''){$this->opt['join']=  " {$type} join ".$this->pex.trim($join)." on {$condition}";return $this;}public function where($where){$this->opt['where']=  ' WHERE '.$where;return $this;}public function order($order){$this->opt['order']=  ' ORDER BY '.$order;return $this;}public function group($group){$this->opt['group']=  ' GROUP BY '.$group;return $this;}public function having($having){$this->opt['having']=  ' having '.$having;return $this;}public function filed($filed){$this->opt['filed']= $filed;return $this;}public function limit($limit){$this->opt['limit']=  ' limit '.$limit;return $this;}public function select(){$sql = "SELECT ".$this->opt['filed']. " FROM ".$this->table.$this->opt['alias'].$this->opt['join'].$this->opt['where'].$this->opt['group'].$this->opt['having'].$this->opt['order'].$this->opt['limit'];$stmt = $this->pdo->prepare($sql);$stmt->execute();return $stmt->fetchAll(PDO::FETCH_ASSOC);}public function find(){$data=$this->limit(1)->select();return current($data);}public function delete(){$sql = "DELETE FROM ".$this->table.$this->opt['where'];$stmt = $this->pdo->prepare($sql);$stmt->execute();return $stmt->rowCount();}public function insert( $data) {// 准备SQL语句$fields = implode(', ', array_keys($data));$values = ':' . implode(', :', array_keys($data));$sql = "INSERT INTO ". $this->table." (".$fields.") VALUES (".$values.")";// 绑定参数并执行SQL语句$stmt = $this->pdo->prepare($sql);foreach ($data as $key => $value) {$stmt->bindValue(':' . $key, $value);}return $stmt->execute();}public function update( $data){if(empty($this->opt['where'])) die('更新语句必须有were条件');$up='';foreach ($data as $k=>$v){$up.="`".$k."`='".$v."'";}$sql="update  ".$this->table.' set '.$up.$this->opt['where'];$stmt = $this->pdo->prepare($sql);$stmt->execute();return $stmt->rowCount();}}

http://www.lryc.cn/news/151450.html

相关文章:

  • 华为OD:敏感字段加密
  • IDEA新建SpringBoot项目时启动编译报错:Error:java: 无效的源发行版: 17
  • 【云原生进阶之PaaS中间件】第一章Redis-2.3.3集群模式
  • 游戏发行商能够提供什么服务?
  • Linux 多进程解决客户端与服务器端通信
  • Scala的模式匹配
  • HttPClient简介及示例:学习如何与Web服务器进行通信
  • STS4 New 安装Spring Bean Configuration File
  • Java经典面试题(异或运算)
  • No primary or single unique constructor found for interface java.util.List
  • C#关于WebService中File.Exists()处理远程路径的异常记录
  • JavaWeb_LeadNews_Day10-Xxljob, Redis实现定时热文章
  • 【WebRTC---源码篇】(二:二)视频源VideoSourceBase
  • Linux_8_磁盘存储和文件系统
  • VS + QT 封装带UI界面的DLL
  • 逆向工程-架构真题(二十)
  • Zookeeper 入门
  • 记录--前端使用a链接下载内容增加loading效果
  • 如何获取用户的ip地址
  • 单片机-控制按键点亮LED灯
  • 微信小程序新版隐私协议弹窗实现最新版
  • GO语言圣经 第五章习题
  • 用kotlin 开发一个简单的多页面跳转
  • 记录我的tensorrt 部署yolov8
  • 什么是用户界面? 优漫动游
  • 基于 Docker 的 MySQL 主从复制搭建(Mac M1版本)
  • 【Locomotor运动模块】瞬移
  • 【负载均衡】常见的负载均衡策略有哪些?
  • ChatGPT如何应对紧急救援和医疗应急?
  • vue3 ref reactive响应式数据 赋值的问题