<?php

namespace app\common\model;
use think\Model;

class Base extends \think\Model{
    // 基础Model超类
    protected function _query($field = '*',$where = [],$page = null,$mapping = []){
        $model = $this::order('id desc');

        $data = [];
        $data['count'] = $this::where($where)->count();
        $data['list'] = $this->all(function($query) use ($where,$page,$field){
            $query->order( 'id desc' );
            $query->where( $where );
            $query->field( $field );
            if( $page && array_key_exists('index',$page) && array_key_exists('size',$page) ){
                $query->limit( ($page['index'] - 1) * $page['size'],$page['size'] );
            }
            // echo $query->getLastSql();
        }, $mapping);
        //return $data;
        return $data['list'];
    }
    // 查询基础方法
    protected function query($field,$where = [],$page = null,$mapping,$order = null){
        return $this->_query($field ,$where ,$page,$mapping,$order);
    }
    // 获取所有院校
    public function get_all($where = [],$field = null,$page = null,$mapping = [],$order = null){
        if(! $field){
            $field = $this->field;
        }
        return $this->query($field,$where,$page,$mapping,$order);//['list'];
    }
    // 实现1对1，1对多的关联，提取method
    protected function get_method(){
        $backtrace = debug_backtrace();
        array_shift($backtrace);
        array_shift($backtrace);
        $mtehods_array = [];
        if(count($backtrace) > 0){
            $functions = $backtrace[0]['function'];                         //得到拼接方法名称
            if( $functions ){
                $item = substr(explode('to',$functions)[1],1);  //拆解
                // var_dump($array);
                // die;
                // if( count($array) > 0){
                //     foreach($array as $item){
                //         array_push($mtehods_array ,ucfirst($item));  
                //     }
                // }
                array_push($mtehods_array ,ucfirst($item));  
            }
            return $mtehods_array;
        }
    }
}