tp5重写model

tp5重写model

自定义base_model 

  1. toWhere(  $where1,$where2 (可选参数) )   限制条件 基于where()的更改,  $where2在联表时可用与写右表的where条件。
  2. toUnset( $data ); 去除数据库不存在的key值。
  3. insertData( $data );  写入方法 与insert()同用。
  4. toField( $where1,$where2 ) 在与toJoin() 连用时 $where2不可缺  $where1为左表的字段  $where2 为右表要查询的字段。
  5. toJoin() 用于两表连查。
  6. toSave( $data ) 用于修改 在与toUnset()一起用过程中 $data需传入toUnset( $data ) 中。】
  7. toOneArr() 查询单条转为数组
  8. toLinked() 引用了 yii框架 容器的概念  对链表进行了管理  抛弃了 tp5中 with()+hasOne()方式

toWhere() + toUnset() + toSave() 连贯使用 

    public function mBindingAdmin($where=[],$data=[]){
        return $this->toWhere($where)->toUnset($data)->toSave();
    }

两个表

a表 

b表

a表主键id = b表a_id

这种进行链表操作,我们在RegisterJoin进行链表配置。

<?php
namespace app\admin\model;

class RegisterJoin{
	// public function __construct(){
	// 	parent::__construct();
	// }
	
	public function JoinMain(){
		return [
			'aa' => 'a_b',

		];
	}
	public function JoinWhere(){
		return [	 //左表明 右表明 左表id 右表id
			'a_b' => ['aa','bb','id','a_id'],

		];
	}
}

然后再你的表model引入RegisterJoin类就完事了。

链表使用toWhere() + toField()

如果你需要查询 左表id=1的 以及 左表 id,name 右表address字段

    public function mGetField($where=[],$data=[]){
        return $this->toWhere(['id'=>1])->toField(['id','name'],['address'])->toOneArr();
    }

如果你需要查询 左表id=1&&右表address='我在家' 和 左表 id,name 右表address字段

    public function mGetField($where=[],$data=[]){
        return $this->toWhere(['id'=>1],['address'=>'我在家'])->toField(['id','name'],['address'])->toOneArr();
    }

BaseModel.php代码

<?php
/**
 * Created by PhpStorm.
 * User: 林虎
 * Date: 2018/7/25
 * Time: 16:25
 */

namespace app\model;


use think\Model;
class BaseModel extends Model{
	static $th;
    private $JoinModelObj;
    private $JoinWhere;
    private $JoinTable;
    private $JoinAs;
    public $SaveData;
    public function insertData($data=[]){
       
        if(empty($data)){
            $data = $this->SaveData;
        }
         unset($data['update_time']);
        $res = $this->insert($data);
        return $res;
    }
    public function toSave($data=''){
        if(!empty($data)){
            $this->update($data);
        }else{

          $this->update($this->SaveData);
        }
        return $this->errMess();;
        
    }
    public function errMess(){
        return empty($this->failException)?true:false;
    }
    public function toUnset($data){
        foreach($data as $k=>$v){
            // !in_array($k, $this->getTableFields())? $data = unset($data[$key]) :'';
            if(!in_array($k, $this->getTableFields())){
                // var_dump($k,$this->getTableFields());die;
                unset($data[$k]);
            }
        }

        $this->SaveData = $data;
        return $this;
    }

    public function toOne(){
    	return $this->find();
    }
    public function toAll(){
    	return $this->select();
    }
    public function toOneArr(){
         
         return json_decode(json_encode($this->find(),JSON_UNESCAPED_UNICODE),true);
    }
    public function toField($field_l,$field_r=''){
    	$str_l = '';
        $str_r = '';
    	if(!empty($field_l) and !empty($field_r)){
    		// $field_l = trim(',',explode(',',array_values($field_l)));
    		foreach ($field_l as $key => $value) {
    			$str_l.= '`'.strtolower($this->name).'`.`'.$value.'`,';
    		}
            foreach ($field_r as $key => $value) {
                $str_r .= '`'.$this->JoinAs[1].'`.`'.$value.'`,';
            }
    	}
        $JoinWhere = $str_l.trim($str_r,',');
    	$this->field($JoinWhere);
    	return $this;
    }
    public function toWhere($data='',$where=''){
    	if(!empty($where)){
    		$this->whereOr($data);
    	}else{
    		$this->where($data);
    	}
        return $this;
    }
    public function toJoinJoin(){

    }
    public function toJoin(){
      $this->toLinked()->join($this->JoinTable,$this->JoinWhere);
      return $this;
    }
    public function toLinked(){
        $this->JoinModelObj = new RegisterJoin();
        $Joinkey= $this->JoinModelObj->JoinMain()[strtolower($this->name)];
        $JoinWhere = $this->JoinModelObj->JoinWhere()[$Joinkey];
        $this->JoinAs = explode('_',$Joinkey);
        $this->JoinTable = strncmp($this->JoinWhere[0],$this->name,100)?$JoinWhere[1].' '.$this->JoinAs[1]:$JoinWhere[0].' '.$this->JoinAs[0];
        $this->JoinWhere = '`qqcard_'.strtolower($this->name).'`.`'.$JoinWhere[2].'`=`'.$this->JoinAs[1].'`.`'.$JoinWhere[3].'`';
        return $this;
    }
}

RegisterJoin.php代码

<?php
namespace app\admin\model;

class RegisterJoin{
	// public function __construct(){
	// 	parent::__construct();
	// }
	
	public function JoinMain(){
		return [
			'aa' => 'a_b',

		];
	}
	public function JoinWhere(){
		return [	 //左表明 右表明 左表id 右表id
			'a_b' => ['aa','bb','id','a_id'],

		];
	}
}

                                                                                                  得意心不傲,措意心不衰。

猜你喜欢

转载自blog.csdn.net/kolinhu/article/details/81200270
tp5