PHP's own framework PDO data table prefix, alias, model, table, join method implementation (Improvement Part 9 - End)

1. Implement functions, implement data table prefix, alias, model, table, and join methods

 

 2. Table prefix implementation

1. Add table prefix to config.php
 'DB_PEX'=>'fa_',//数据库前缀
 2. Add table prefix method function.php
function model($table){
    $model=new  ModelBase($table,config("DB_PEX"));
    return $model;
}
function table($table){
    return  new  ModelBase($table);
}
3. Add the table prefix ModelBase.php to the PDO data table
    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();
    }

3. alias data table alias and join implementation to implement 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);
    }

4. Controller implementation, data table prefix, alias, model, table, join method query

 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);
    }

5. Complete ModelBase.php code

<?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();
    }

}

Guess you like

Origin blog.csdn.net/weixin_39934453/article/details/132550309