From 241204045735a7ee90a5d826ee9198cf1a478990 Mon Sep 17 00:00:00 2001 From: top_nuomi <1130395124@qq.com> Date: Mon, 2 Sep 2019 17:44:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9C=A8=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=8F=8ADatabase=E7=B1=BB=E4=B8=AD=E5=AE=9A=E4=B9=89=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=93=8D=E4=BD=9C=E6=95=B0=E6=8D=AE=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/Database.php | 127 ++++++++++++++++++++++----------- framework/library/Model.php | 82 ++++++++++++++------- 2 files changed, 143 insertions(+), 66 deletions(-) diff --git a/framework/library/Database.php b/framework/library/Database.php index a8136d3..b089fb4 100644 --- a/framework/library/Database.php +++ b/framework/library/Database.php @@ -11,61 +11,122 @@ use top\library\database\ifs\DatabaseIfs; class Database { - // 数据库驱动 - private static $driver; + /** + * 数据库驱动 + * @var null + */ + private static $driver = null; - // 当前类实例 + /** + * 当前类实例 + * @var array + */ private static $instance = []; - // 当前表结构 + /** + * 当前表结构 + * @var array + */ private static $tableDesc = []; - // 数据库配置 + /** + * 数据库配置 + * @var array + */ private $config = []; - // 当前操作的表 + /** + * 当前操作的表 + * @var string + */ private $table = ''; - // 当前表的主键 + /** + * 当前表的主键 + * @var string + */ 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 $order = ''; + /** + * 排序 + * @var null + */ + private $order = null; - // 范围 - private $limit = ''; + /** + * 范围 + * @var null + */ + private $limit = null; - // 多表 + /** + * 多表 + * @var array + */ private $join = []; - // 关联 + /** + * 关联 + * @var array + */ private $on = []; /** * Database constructor. * @param $table * @param $pk + * @param $prefix + * @throws \Exception */ - private function __construct($table, $pk) + private function __construct($table, $pk, $prefix) { $driver = Register::get('DBDriver'); $this->config = $config = Config::instance()->get('db'); - $this->table = $config['prefix'] . $table; + $this->table = (($prefix) ? $prefix : $config['prefix']) . $table; $this->pk = $pk; $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); } - /** - * 指定表 - * @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 @@ -398,14 +445,14 @@ class Database */ private function _reset() { - $this->effect = ''; - $this->distinct = ''; - $this->field = ''; + $this->effect = null; + $this->distinct = null; + $this->field = null; $this->join = []; $this->on = []; $this->where = []; - $this->order = ''; - $this->limit = ''; + $this->order = null; + $this->limit = null; $this->table = str_ireplace(' as this', '', $this->table); } diff --git a/framework/library/Model.php b/framework/library/Model.php index f7dae76..7a82f9b 100644 --- a/framework/library/Model.php +++ b/framework/library/Model.php @@ -9,36 +9,66 @@ namespace top\library; class Model { - // 数据库操作实例 - private $db; + /** + * 当前表名 + * @var string + */ + protected $table = ''; - // 当前表名 - protected $table; - - // 主键 + /** + * 主键 + * @var string + */ protected $pk = ''; - // 字段映射 + /** + * 当前模型的表前缀 + * @var string + */ + protected $prefix = ''; + + /** + * 字段映射 + * @var array + */ protected $map = []; - // insert值映射 + /** + * insert值映射 + * @var array + */ protected $inReplace = []; - // update值映射 + /** + * update值映射 + * @var array + */ protected $updateReplace = []; - // 出库值映射 + /** + * 出库值映射 + * @var array + */ protected $outReplace = []; - // 模型消息(请注意:在方法中赋值会覆盖掉数据验证的message) + /** + * 模型消息(请注意:在方法中赋值会覆盖掉数据验证的message) + * @var string + */ protected $message = ''; - // 自动验证 + /** + * 自动验证 + * @var array + */ protected $validate = []; - // 是否为insert操作,决定如何验证数据 - // true:验证模型中配置的全部字段 - // false:仅验证$data中存在的字段 + /** + * 是否为insert操作,决定如何验证数据 + * true:验证模型中配置的全部字段 + * false:仅验证$data中存在的字段 + * @var bool + */ private $isInsert = false; /** @@ -63,7 +93,7 @@ class Model */ 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 */ - public function count($param = '') + public function count($param = null) { return $this->getDb()->common($param, 'count'); } /** * 平均值 - * @param string $param + * @param null $param * @return mixed */ - public function avg($param = '') + public function avg($param = null) { return $this->getDb()->common($param, 'avg'); } /** * 最大值 - * @param string $param + * @param null $param * @return mixed */ - public function max($param = '') + public function max($param = null) { return $this->getDb()->common($param, 'max'); } /** * 最小值 - * @param string $param + * @param null $param * @return mixed */ - public function min($param = '') + public function min($param = null) { return $this->getDb()->common($param, 'min'); } /** * 求和 - * @param string $param + * @param null $param * @return mixed */ - public function sum($param = '') + public function sum($param = null) { return $this->getDb()->common($param, 'sum'); }