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

43 篇文章 1 订阅
23 篇文章 2 订阅
一、实现功能,数据表前缀、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();
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP隔壁老王邻居

啦啦啦啦啦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值