|
@@ -21,7 +21,7 @@ class Base {
|
|
|
/**
|
|
|
* @var Model $db
|
|
|
*/
|
|
|
- public static $db = null;
|
|
|
+ public $db = null;
|
|
|
/**
|
|
|
* 主键
|
|
|
* @var mix $privateKeys null
|
|
@@ -83,6 +83,8 @@ class Base {
|
|
|
*/
|
|
|
public $__Error;
|
|
|
|
|
|
+ public $fetchSQL = false;
|
|
|
+
|
|
|
public function __construct(){
|
|
|
$this->cleanVars();
|
|
|
}
|
|
@@ -90,13 +92,14 @@ class Base {
|
|
|
/**
|
|
|
* 初始化的时候清除所有静态变量的值
|
|
|
*
|
|
|
- * @return void
|
|
|
+ * @return $this
|
|
|
*/
|
|
|
final public function cleanVars() {
|
|
|
$this->properties = [];
|
|
|
$this->uniqKeys = null;
|
|
|
$this->privateKeys = null;
|
|
|
$this->exclude = [];
|
|
|
+ return $this;
|
|
|
}
|
|
|
/**
|
|
|
* 默认值,仅在新增数据的时候支持,更新不支持
|
|
@@ -112,7 +115,7 @@ class Base {
|
|
|
* @return $this
|
|
|
*/
|
|
|
public function setTableName($tableName) {
|
|
|
- if(is_callable($tableName)) {
|
|
|
+ if(is_callable($tableName, true)) {
|
|
|
$this->tableName = $tableName();
|
|
|
}
|
|
|
if($tableName) $this->tableName = $tableName;
|
|
@@ -210,7 +213,7 @@ class Base {
|
|
|
$or = [];
|
|
|
if(isset($this->hooker['or'])) {
|
|
|
foreach ($this->hooker['or'] as $value) {
|
|
|
- if(is_callable($value['func'])) {
|
|
|
+ if(is_callable($value['func'], true)) {
|
|
|
$or[] = call_user_func($value['func'], [], $value['args']);
|
|
|
}
|
|
|
}
|
|
@@ -272,7 +275,7 @@ class Base {
|
|
|
}
|
|
|
// 遍历
|
|
|
foreach ($this->hooker['where'] as $item) {
|
|
|
- if(is_callable($item['func'])) {
|
|
|
+ if(is_callable($item['func'], true)) {
|
|
|
$subWhere = call_user_func($item['func'], $where, $item['args']);
|
|
|
foreach ($subWhere as $key => $value) {
|
|
|
if(!preg_match("/^[0-9]/", $key) && is_array($value) && count($value, 1) != count($value)) {
|
|
@@ -346,7 +349,7 @@ class Base {
|
|
|
$join = [];
|
|
|
if(isset($this->hooker['join'])) {
|
|
|
foreach ($this->hooker['join'] as $value) {
|
|
|
- if(is_callable($value['func'])) {
|
|
|
+ if(is_callable($value['func'], true)) {
|
|
|
$join[] = call_user_func($value['func'], [], $value['args']);
|
|
|
}
|
|
|
}
|
|
@@ -441,7 +444,7 @@ class Base {
|
|
|
* @return int|mixed
|
|
|
*/
|
|
|
public function getLimitHooker() {
|
|
|
- if(isset($this->hooker['limit']) && is_callable($this->hooker['limit']['func'])) {
|
|
|
+ if(isset($this->hooker['limit']) && is_callable($this->hooker['limit']['func'], true)) {
|
|
|
return call_user_func($this->hooker['limit']['func'], [], $this->hooker['limit']['args']);
|
|
|
}
|
|
|
return [];
|
|
@@ -479,7 +482,7 @@ class Base {
|
|
|
* @return mixed|null[]
|
|
|
*/
|
|
|
public function getCacheHooker() {
|
|
|
- if(isset($this->hooker['cache']) && is_callable($this->hooker['cache']['func'])) {
|
|
|
+ if(isset($this->hooker['cache']) && is_callable($this->hooker['cache']['func'], true)) {
|
|
|
return call_user_func($this->hooker['cache']['func'], [], $this->hooker['cache']['args']);
|
|
|
}
|
|
|
return [null, null, null];
|
|
@@ -549,7 +552,7 @@ class Base {
|
|
|
$callback = [];
|
|
|
if(isset($this->hooker['with'])) {
|
|
|
foreach ($this->hooker['with'] AS $hooker) {
|
|
|
- if(is_callable($hooker['func'])) {
|
|
|
+ if(is_callable($hooker['func'], true)) {
|
|
|
$callback[] = call_user_func($hooker['func'], [], $hooker['args']);
|
|
|
}
|
|
|
}
|
|
@@ -564,7 +567,7 @@ class Base {
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
private function checkCallable($hooker) {
|
|
|
- if($hooker && !is_callable($hooker)) {
|
|
|
+ if($hooker && !is_callable($hooker, true)) {
|
|
|
throw new \Exception('Hooker is uncallable');
|
|
|
}
|
|
|
}
|
|
@@ -575,7 +578,7 @@ class Base {
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function setQueryFieldsHooker($hooker, $args = null) {
|
|
|
- if(!is_callable($hooker)) {
|
|
|
+ if(!is_callable($hooker, true)) {
|
|
|
throw new \Exception('Hooker is un callable');
|
|
|
}
|
|
|
$this->setHooker('fields', $hooker, $args);
|
|
@@ -587,7 +590,7 @@ class Base {
|
|
|
* @return mixed|string
|
|
|
*/
|
|
|
public function getFieldsHooker() {
|
|
|
- if(isset($this->hooker['fields']) && is_callable($this->hooker['fields']['func'])) {
|
|
|
+ if(isset($this->hooker['fields']) && is_callable($this->hooker['fields']['func'], true)) {
|
|
|
return call_user_func($this->hooker['fields']['func'], [], $this->hooker['fields']['args']);
|
|
|
}
|
|
|
return '*';
|
|
@@ -611,10 +614,11 @@ class Base {
|
|
|
* @return \Qii\Driver\Base
|
|
|
*/
|
|
|
public function db() {
|
|
|
- if(self::$db !== null && self::$db instanceof Model) {
|
|
|
- return self::$db;
|
|
|
+ if($this->db !== null && $this->db instanceof Model) {
|
|
|
+ return $this->db;
|
|
|
}
|
|
|
- return _loadClass('\Qii\Driver\Model');
|
|
|
+ $this->db = _loadClass('\Qii\Driver\Model');
|
|
|
+ return $this->db;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -628,14 +632,14 @@ class Base {
|
|
|
if(!($db instanceof \Qii\Driver\Model)) {
|
|
|
throw new \Exception('DB is illegal');
|
|
|
}
|
|
|
- self::$db = $db;
|
|
|
+ $this->db = $db;
|
|
|
}
|
|
|
/**
|
|
|
* 返回 entity 信息
|
|
|
* @return \Qii\Driver\Entity\Entity
|
|
|
*/
|
|
|
public function entity() {
|
|
|
- return _loadClass('\Qii\Driver\Entity\Entity');
|
|
|
+ return _loadClassOnce('\Qii\Driver\Entity\Entity');
|
|
|
}
|
|
|
/**
|
|
|
* 设置查询字段
|
|
@@ -1499,6 +1503,16 @@ class Base {
|
|
|
final public function endRecord(){
|
|
|
$this->db()->endRecord();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否只返回SQL
|
|
|
+ * @param $fetch
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ final public function fetchSQL($fetch = false) {
|
|
|
+ $this->fetchSQL = $fetch;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取where及or条件并创建查询条件
|
|
|
*
|
|
@@ -1508,7 +1522,7 @@ class Base {
|
|
|
if(empty($fields)) {
|
|
|
$fields = $this->getFields();
|
|
|
}
|
|
|
- $query = $this->db()->fields($fields)->where($this->getWhereHooker())->fetchSql($this->calledSQL);
|
|
|
+ $query = $this->db()->fields($fields)->where($this->getWhereHooker())->fetchSQL($this->fetchSQL);
|
|
|
$or = $this->getOrHooker();
|
|
|
foreach ($or as $v) {
|
|
|
$query = $query->orTerms($v);
|
|
@@ -1663,7 +1677,6 @@ class Base {
|
|
|
}
|
|
|
return $query->rs($this->prepareTable());
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* select rows
|
|
|
* @param int $page 页码
|
|
@@ -1688,6 +1701,20 @@ class Base {
|
|
|
return $lists;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取一列
|
|
|
+ *
|
|
|
+ * @return array|false
|
|
|
+ * @throws \Qii\Exceptions\TableException
|
|
|
+ */
|
|
|
+ final public function getOne() {
|
|
|
+ $query = $this->createQuery();
|
|
|
+ $query = $query->groupBy($this->getGroupBy());
|
|
|
+ $query = $query->orderBy($this->getOrderBy());
|
|
|
+ $query->limit(0, 1);
|
|
|
+ return $query->selectOne($this->prepareTable());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取一行
|
|
|
*
|
|
@@ -2056,7 +2083,7 @@ class Base {
|
|
|
public function getOrderBy() {
|
|
|
$order = array();
|
|
|
$orderBy = $this->orderBy();
|
|
|
- if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'])) {
|
|
|
+ if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'], true)) {
|
|
|
$orderBy = call_user_func($this->hooker['order']['func'], $order, $this->hooker['order']['args']);
|
|
|
}
|
|
|
foreach ($orderBy as $key => $val) {
|