数据库

面向对象-链式函数写法与使用

2017-12-18  本文已影响9人  云龙789

举例

$obj = new Obj();
$name = $obj->table('test')->where('id','>','3')->get(['id','time']);

链式函数其实是每次执行这个函数的时候,
1.给这个函数中的属性赋值,
2.返回本类return $this

下面的一个类是模仿类似于laravel DB类的一种写法

<?php
class Obj
{
    private $sql_connect;
    private $where = '';

    /**
     * Obj constructor. 我这里是用了直接连接好的mysql变量
     */
    public function __construct()
    {
        global $sql_connect;
        $this->sql_connect = $sql_connect;
    }

    public  function table($table)
    {
        $this->table = $table;
        return $this;
    }
    public function where($k='',$gt='=',$v='')
    {
        // 如果有传递参数,则给$this->>where 赋值 ,
        if ($k){
            if (!$v){
                $this->where = "where $k = $gt";
            }else{
                $this->where = "where $k $gt $v";
            }
        }
        // 否则,就不用做处理,因为有 where属性默认值是空  where=''
        return $this;
    }

    public function get($val='*')
    {
        // 如果是数组,则拆分,不是数组则取值全部
        if ($val && is_array($val)){
            $val = implode(',',$val);
        }
        $res = $this->le_db1->query("select $val from $this->table $this->where");
        // 去掉数字的键
        $array = $res->fetchAll();
        foreach ($array as $key=>$value){
            foreach ($value as $k => $val){
                if (is_numeric($k)){
                    unset($array[$key][$k]);
                }
            }
        }
        return $array;
    }
    public function insert($arr)
    {
        // 判断是一维还是二维数组
        if (count($arr) == count($arr,1)){
            // 一维数组
            $keys = array_keys($arr);
            $from = '('.implode(',',$keys).')';
            $to = '(\''.implode('\',\'',$arr).'\')';
        }else{
            // 二维数组  取出第一组数组中的键就是要插入数据的键
            $keys = array_keys($arr[0]);
            $to = '';
            $from = '('.implode(',',$keys).')';
            foreach ($arr as $key => $value){
                $to = $to.'(\''.implode('\',\'',$value).'\'),';
            }
            $to = rtrim($to,',');
        }
        $this->le_db1->query("insert into $this->table $from VALUES $to");
        // 返回最后一个id
        return $this->le_db1->lastInsertId(); 
    }
    public function update($arr)
    {
        $set = '';
        foreach ($arr as $key=>$value){
          // 如果是字符串,要添加引号
            if (is_string($value)){
                $value = "'$value'";
            }
            $set = $set.$key.'='.$value.',';
        }
        $set = rtrim($set,',');
        $res = $this->le_db1->query("update $this->table set $set $this->where");
        return $res->rowCount();
    }

    public function delete()
    {
        // 防止删除全部表,此处可以做where限制
        if (!$this->where){
            return '请谨慎操作,不要删除全部表数据';
        }
        $res = $this->le_db1->query("delete  from $this->table  $this->where");
        return $res->rowCount();
    }
}

注意事项

  1. 获取id>3'id','time'字段
$obj = new Obj();
$res = $obj->table('test')->where('id','>','3')->get(['id','time']);
echo '<pre>';
print_r($res);

2.获取全部数据

$obj = new Obj();
$res = $obj->table('test')->where()->get();
或者
$res = $obj->table('test')->get();

插入数据的使用

$obj = new Obj();
$res = $obj->table('test')->insert([['test'=>'4','time'=>'4'],['test'=>'5','time'=>'5'],['test'=>'6','time'=>'6']]);
$res = $obj->table('test')->insert(['test'=>'4','time'=>'4']);

删除
$res = $obj->table('test')->where('id',2)->delete();
更新
$res = $obj->table('test')->where('id',3)->update(['test'=>4]);
上一篇 下一篇

猜你喜欢

热点阅读