支持在模型及Database类中定义当前操作数据表的前缀

This commit is contained in:
TOP糯米 2019-09-02 17:44:16 +08:00
parent b9579677a6
commit 2412040457
2 changed files with 143 additions and 66 deletions

View File

@ -11,61 +11,122 @@ use top\library\database\ifs\DatabaseIfs;
class Database class Database
{ {
// 数据库驱动 /**
private static $driver; * 数据库驱动
* @var null
*/
private static $driver = null;
// 当前类实例 /**
* 当前类实例
* @var array
*/
private static $instance = []; private static $instance = [];
// 当前表结构 /**
* 当前表结构
* @var array
*/
private static $tableDesc = []; private static $tableDesc = [];
// 数据库配置 /**
* 数据库配置
* @var array
*/
private $config = []; private $config = [];
// 当前操作的表 /**
* 当前操作的表
* @var string
*/
private $table = ''; private $table = '';
// 当前表的主键 /**
* 当前表的主键
* @var string
*/
private $pk = ''; private $pk = '';
// 多个表仅delete操作 /**
private $effect = ''; * 多个表仅delete操作
* @var null
*/
private $effect = null;
private $distinct = ''; /**
* 数据去重
* @var null
*/
private $distinct = null;
// 操作的字段 /**
private $field = ''; * 操作的字段
* @var null
*/
private $field = null;
// 条件 /**
* 条件
* @var array
*/
private $where = []; private $where = [];
// 排序 /**
private $order = ''; * 排序
* @var null
*/
private $order = null;
// 范围 /**
private $limit = ''; * 范围
* @var null
*/
private $limit = null;
// 多表 /**
* 多表
* @var array
*/
private $join = []; private $join = [];
// 关联 /**
* 关联
* @var array
*/
private $on = []; private $on = [];
/** /**
* Database constructor. * Database constructor.
* @param $table * @param $table
* @param $pk * @param $pk
* @param $prefix
* @throws \Exception
*/ */
private function __construct($table, $pk) private function __construct($table, $pk, $prefix)
{ {
$driver = Register::get('DBDriver'); $driver = Register::get('DBDriver');
$this->config = $config = Config::instance()->get('db'); $this->config = $config = Config::instance()->get('db');
$this->table = $config['prefix'] . $table; $this->table = (($prefix) ? $prefix : $config['prefix']) . $table;
$this->pk = $pk; $this->pk = $pk;
$this->setDriver($driver, $this->config); $this->setDriver($driver, $this->config);
} }
/**
* 指定表
* @param $table
* @param string $pk
* @param string $prefix
* @return mixed
* @throws \Exception
*/
public static function table($table, $pk = '', $prefix = '')
{
if (!isset(self::$instance[$table])) {
self::$instance[$table] = new self($table, $pk, $prefix);
}
return self::$instance[$table];
}
/** /**
* 指定数据库驱动 * 指定数据库驱动
* *
@ -77,20 +138,6 @@ class Database
self::$driver = $driver->connect($config); self::$driver = $driver->connect($config);
} }
/**
* 指定表
* @param $table
* @param string $pk
* @return mixed
*/
public static function table($table, $pk = '')
{
if (!isset(self::$instance[$table])) {
self::$instance[$table] = new self($table, $pk);
}
return self::$instance[$table];
}
/** /**
* 指定多张表 * 指定多张表
* @param $effect * @param $effect
@ -398,14 +445,14 @@ class Database
*/ */
private function _reset() private function _reset()
{ {
$this->effect = ''; $this->effect = null;
$this->distinct = ''; $this->distinct = null;
$this->field = ''; $this->field = null;
$this->join = []; $this->join = [];
$this->on = []; $this->on = [];
$this->where = []; $this->where = [];
$this->order = ''; $this->order = null;
$this->limit = ''; $this->limit = null;
$this->table = str_ireplace(' as this', '', $this->table); $this->table = str_ireplace(' as this', '', $this->table);
} }

View File

@ -9,36 +9,66 @@ namespace top\library;
class Model class Model
{ {
// 数据库操作实例 /**
private $db; * 当前表名
* @var string
*/
protected $table = '';
// 当前表名 /**
protected $table; * 主键
* @var string
// 主键 */
protected $pk = ''; protected $pk = '';
// 字段映射 /**
* 当前模型的表前缀
* @var string
*/
protected $prefix = '';
/**
* 字段映射
* @var array
*/
protected $map = []; protected $map = [];
// insert值映射 /**
* insert值映射
* @var array
*/
protected $inReplace = []; protected $inReplace = [];
// update值映射 /**
* update值映射
* @var array
*/
protected $updateReplace = []; protected $updateReplace = [];
// 出库值映射 /**
* 出库值映射
* @var array
*/
protected $outReplace = []; protected $outReplace = [];
// 模型消息请注意在方法中赋值会覆盖掉数据验证的message /**
* 模型消息请注意在方法中赋值会覆盖掉数据验证的message
* @var string
*/
protected $message = ''; protected $message = '';
// 自动验证 /**
* 自动验证
* @var array
*/
protected $validate = []; protected $validate = [];
// 是否为insert操作决定如何验证数据 /**
// true验证模型中配置的全部字段 * 是否为insert操作决定如何验证数据
// false仅验证$data中存在的字段 * true:验证模型中配置的全部字段
* false:仅验证$data中存在的字段
* @var bool
*/
private $isInsert = false; private $isInsert = false;
/** /**
@ -63,7 +93,7 @@ class Model
*/ */
private function getDb() private function getDb()
{ {
return Database::table($this->table, $this->pk); return Database::table($this->table, $this->pk, $this->prefix);
} }
/** /**
@ -245,50 +275,50 @@ class Model
/** /**
* 计数 * 计数
* @param string $param * @param null $param
* @return mixed * @return mixed
*/ */
public function count($param = '') public function count($param = null)
{ {
return $this->getDb()->common($param, 'count'); return $this->getDb()->common($param, 'count');
} }
/** /**
* 平均值 * 平均值
* @param string $param * @param null $param
* @return mixed * @return mixed
*/ */
public function avg($param = '') public function avg($param = null)
{ {
return $this->getDb()->common($param, 'avg'); return $this->getDb()->common($param, 'avg');
} }
/** /**
* 最大值 * 最大值
* @param string $param * @param null $param
* @return mixed * @return mixed
*/ */
public function max($param = '') public function max($param = null)
{ {
return $this->getDb()->common($param, 'max'); return $this->getDb()->common($param, 'max');
} }
/** /**
* 最小值 * 最小值
* @param string $param * @param null $param
* @return mixed * @return mixed
*/ */
public function min($param = '') public function min($param = null)
{ {
return $this->getDb()->common($param, 'min'); return $this->getDb()->common($param, 'min');
} }
/** /**
* 求和 * 求和
* @param string $param * @param null $param
* @return mixed * @return mixed
*/ */
public function sum($param = '') public function sum($param = null)
{ {
return $this->getDb()->common($param, 'sum'); return $this->getDb()->common($param, 'sum');
} }