|
@@ -1,9 +1,7 @@
|
|
<?php
|
|
<?php
|
|
namespace Qii\Driver\Entity;
|
|
namespace Qii\Driver\Entity;
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-use Qii\Driver\Response;
|
|
|
|
|
|
+use \Qii\Driver\Response;
|
|
|
|
|
|
class Base {
|
|
class Base {
|
|
/**
|
|
/**
|
|
@@ -54,6 +52,7 @@ class Base {
|
|
protected function setHooker($key, $hooker, $args) {
|
|
protected function setHooker($key, $hooker, $args) {
|
|
$this->hooker[$key]['func'] = $hooker;
|
|
$this->hooker[$key]['func'] = $hooker;
|
|
$this->hooker[$key]['args'] = $args;
|
|
$this->hooker[$key]['args'] = $args;
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -62,9 +61,10 @@ class Base {
|
|
* @return void
|
|
* @return void
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function setWhereHooker($hooker, $args = null) {
|
|
|
|
|
|
+ final public function setWhereHooker($hooker, $args = null) {
|
|
$this->checkCallable($hooker);
|
|
$this->checkCallable($hooker);
|
|
$this->setHooker('where', $hooker, $args);
|
|
$this->setHooker('where', $hooker, $args);
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 获取where条件
|
|
* 获取where条件
|
|
@@ -85,9 +85,10 @@ class Base {
|
|
* @return void
|
|
* @return void
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function setOrderHooker($hooker, $args = null) {
|
|
|
|
|
|
+ final public function setOrderHooker($hooker, $args = null) {
|
|
$this->checkCallable($hooker);
|
|
$this->checkCallable($hooker);
|
|
$this->setHooker('order', $hooker, $args);
|
|
$this->setHooker('order', $hooker, $args);
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -97,15 +98,16 @@ class Base {
|
|
* @return void
|
|
* @return void
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function setLimitHooker($hooker, $args = null) {
|
|
|
|
|
|
+ final public function setLimitHooker($hooker, $args = null) {
|
|
$this->checkCallable($hooker);
|
|
$this->checkCallable($hooker);
|
|
- $this->setHooker('order', $hooker, $args);
|
|
|
|
|
|
+ $this->setHooker('limit', $hooker, $args);
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 获取limit
|
|
* 获取limit
|
|
* @return int|mixed
|
|
* @return int|mixed
|
|
*/
|
|
*/
|
|
- public function getLimitHooker() {
|
|
|
|
|
|
+ final public function getLimitHooker() {
|
|
if($this->hooker['limit'] && is_callable($this->hooker['limit']['func'])) {
|
|
if($this->hooker['limit'] && is_callable($this->hooker['limit']['func'])) {
|
|
return call_user_func($this->hooker['limit']['func'], [], $this->hooker['limit']['args']);
|
|
return call_user_func($this->hooker['limit']['func'], [], $this->hooker['limit']['args']);
|
|
}
|
|
}
|
|
@@ -120,16 +122,17 @@ class Base {
|
|
* @return void
|
|
* @return void
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function setCacheHooker($hooker, $args = null) {
|
|
|
|
|
|
+ final public function setCacheHooker($hooker, $args = null) {
|
|
$this->checkCallable($hooker);
|
|
$this->checkCallable($hooker);
|
|
$this->setHooker('cache', $hooker, $args);
|
|
$this->setHooker('cache', $hooker, $args);
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取Cache Hooker
|
|
* 获取Cache Hooker
|
|
* @return mixed|null[]
|
|
* @return mixed|null[]
|
|
*/
|
|
*/
|
|
- public function getCacheHooker() {
|
|
|
|
|
|
+ final public function getCacheHooker() {
|
|
if($this->hooker['cache'] && is_callable($this->hooker['cache']['func'])) {
|
|
if($this->hooker['cache'] && is_callable($this->hooker['cache']['func'])) {
|
|
return call_user_func($this->hooker['cache']['func'], [], $this->hooker['cache']['args']);
|
|
return call_user_func($this->hooker['cache']['func'], [], $this->hooker['cache']['args']);
|
|
}
|
|
}
|
|
@@ -142,7 +145,7 @@ class Base {
|
|
* @param array $config 缓存配置
|
|
* @param array $config 缓存配置
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
- public function setCacheConfig($config) {
|
|
|
|
|
|
+ final public function setCacheConfig($config) {
|
|
if(!is_array($config)) {
|
|
if(!is_array($config)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -154,7 +157,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public function getCacheConfig() {
|
|
|
|
|
|
+ final public function getCacheConfig() {
|
|
if(is_array(self::$config) && isset(self::$config['cache']) && is_array(self::$config['cache'])) {
|
|
if(is_array(self::$config) && isset(self::$config['cache']) && is_array(self::$config['cache'])) {
|
|
return self::$config['cache'];
|
|
return self::$config['cache'];
|
|
}
|
|
}
|
|
@@ -178,11 +181,12 @@ class Base {
|
|
* @return void
|
|
* @return void
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function setQueryFieldsHooker($hooker, $args = null) {
|
|
|
|
|
|
+ final public function setQueryFieldsHooker($hooker, $args = null) {
|
|
if(!is_callable($hooker)) {
|
|
if(!is_callable($hooker)) {
|
|
throw new \Exception('Hooker is un callable');
|
|
throw new \Exception('Hooker is un callable');
|
|
}
|
|
}
|
|
$this->setHooker('fields', $hooker, $args);
|
|
$this->setHooker('fields', $hooker, $args);
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -190,7 +194,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return mixed|string
|
|
* @return mixed|string
|
|
*/
|
|
*/
|
|
- public function getFieldsHooker() {
|
|
|
|
|
|
+ final public function getFieldsHooker() {
|
|
if($this->hooker['fields'] && is_callable($this->hooker['fields']['func'])) {
|
|
if($this->hooker['fields'] && is_callable($this->hooker['fields']['func'])) {
|
|
return call_user_func($this->hooker['fields']['func'], [], $this->hooker['fields']['args']);
|
|
return call_user_func($this->hooker['fields']['func'], [], $this->hooker['fields']['args']);
|
|
}
|
|
}
|
|
@@ -202,7 +206,7 @@ class Base {
|
|
* @param string $field
|
|
* @param string $field
|
|
* @return mixed
|
|
* @return mixed
|
|
*/
|
|
*/
|
|
- public function getFieldVal($field) {
|
|
|
|
|
|
+ final public function getFieldVal($field) {
|
|
if(!preg_match('/^[a-zA-Z]/', $field)) {
|
|
if(!preg_match('/^[a-zA-Z]/', $field)) {
|
|
throw new \Exception('Field is illegal');
|
|
throw new \Exception('Field is illegal');
|
|
}
|
|
}
|
|
@@ -214,7 +218,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return mixed
|
|
* @return mixed
|
|
*/
|
|
*/
|
|
- public function db() {
|
|
|
|
|
|
+ final public function db() {
|
|
return _loadClass('\Qii\Driver\Model');
|
|
return _loadClass('\Qii\Driver\Model');
|
|
}
|
|
}
|
|
|
|
|
|
@@ -222,7 +226,7 @@ class Base {
|
|
* 返回 entity 信息
|
|
* 返回 entity 信息
|
|
* @return mixed
|
|
* @return mixed
|
|
*/
|
|
*/
|
|
- public function entity() {
|
|
|
|
|
|
+ final public function entity() {
|
|
return _loadClass('\Qii\Driver\Entity\Entity');
|
|
return _loadClass('\Qii\Driver\Entity\Entity');
|
|
}
|
|
}
|
|
|
|
|
|
@@ -308,7 +312,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public function properties() {
|
|
|
|
|
|
+ final public function properties() {
|
|
$class = get_called_class();
|
|
$class = get_called_class();
|
|
$method = new \ReflectionClass($class);
|
|
$method = new \ReflectionClass($class);
|
|
$properties = $method->getproperties();
|
|
$properties = $method->getproperties();
|
|
@@ -331,7 +335,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public function getDefaultValue() {
|
|
|
|
|
|
+ final public function getDefaultValue() {
|
|
$default = [];
|
|
$default = [];
|
|
$defaultValue = $this->defaultFieldsValue();
|
|
$defaultValue = $this->defaultFieldsValue();
|
|
if(!$defaultValue || !is_array($defaultValue) || count($defaultValue) == 0) {
|
|
if(!$defaultValue || !is_array($defaultValue) || count($defaultValue) == 0) {
|
|
@@ -350,7 +354,7 @@ class Base {
|
|
* @return mixed
|
|
* @return mixed
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function count() {
|
|
|
|
|
|
+ final public function count() {
|
|
return $this->db()->fields(' COUNT(1) as count')->where($this->getWhereHooker())->selectOne($this->getTable());
|
|
return $this->db()->fields(' COUNT(1) as count')->where($this->getWhereHooker())->selectOne($this->getTable());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -360,7 +364,7 @@ class Base {
|
|
* @param $key
|
|
* @param $key
|
|
* @return bool
|
|
* @return bool
|
|
*/
|
|
*/
|
|
- public function hasProperty($key) {
|
|
|
|
|
|
+ final public function hasProperty($key) {
|
|
$class = get_called_class();
|
|
$class = get_called_class();
|
|
$key = $this->entity()->convertToProperty($key);
|
|
$key = $this->entity()->convertToProperty($key);
|
|
if(isset(self::$properties[$class])) {
|
|
if(isset(self::$properties[$class])) {
|
|
@@ -384,7 +388,7 @@ class Base {
|
|
* @param $values
|
|
* @param $values
|
|
* @return $this|false
|
|
* @return $this|false
|
|
*/
|
|
*/
|
|
- public function bindValues($values) {
|
|
|
|
|
|
+ final public function bindValues($values) {
|
|
if(!is_array($values)) {
|
|
if(!is_array($values)) {
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
@@ -402,7 +406,7 @@ class Base {
|
|
* @return Object
|
|
* @return Object
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function get() {
|
|
|
|
|
|
+ final public function get() {
|
|
$class = get_called_class();
|
|
$class = get_called_class();
|
|
$obj = new $class();
|
|
$obj = new $class();
|
|
$response = $this->info();
|
|
$response = $this->info();
|
|
@@ -424,7 +428,7 @@ class Base {
|
|
* 与get不同的这个返回的是Response
|
|
* 与get不同的这个返回的是Response
|
|
* @return mixed|\Qii\Driver\Qii\Driver\Response
|
|
* @return mixed|\Qii\Driver\Qii\Driver\Response
|
|
*/
|
|
*/
|
|
- public function info() {
|
|
|
|
|
|
+ final public function info() {
|
|
try{
|
|
try{
|
|
$info = $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->selectRow($this->getTable());
|
|
$info = $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->selectRow($this->getTable());
|
|
return Response::Success(static::class .'::'. __FUNCTION__,
|
|
return Response::Success(static::class .'::'. __FUNCTION__,
|
|
@@ -441,7 +445,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return bool
|
|
* @return bool
|
|
*/
|
|
*/
|
|
- public function exist() {
|
|
|
|
|
|
+ final public function exist() {
|
|
$where = $this->getWhereHooker();
|
|
$where = $this->getWhereHooker();
|
|
if(!$where) {
|
|
if(!$where) {
|
|
return false;
|
|
return false;
|
|
@@ -451,16 +455,16 @@ class Base {
|
|
/**
|
|
/**
|
|
* 保存数据
|
|
* 保存数据
|
|
*
|
|
*
|
|
- * @return mixed|Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function add() {
|
|
|
|
|
|
+ final public function add() {
|
|
$valid = $this->validFieldsForAdd();
|
|
$valid = $this->validFieldsForAdd();
|
|
if($valid->isError()) {
|
|
if($valid->isError()) {
|
|
return $valid;
|
|
return $valid;
|
|
}
|
|
}
|
|
//如果设置了 unique 就先验证唯一性
|
|
//如果设置了 unique 就先验证唯一性
|
|
- list($uniqueWhere, $uniqueOr, $exclude, $primary) = $this->condition();
|
|
|
|
|
|
+ list($uniqueWhere, $exclude, $primary) = $this->condition();
|
|
unset($exclude);
|
|
unset($exclude);
|
|
|
|
|
|
//如果 $where $or 为空,看看主键是否有设置值,有设置值说明验证主键就可以,反之设置了主键,主键值设置了,只验证主键就可以了
|
|
//如果 $where $or 为空,看看主键是否有设置值,有设置值说明验证主键就可以,反之设置了主键,主键值设置了,只验证主键就可以了
|
|
@@ -473,8 +477,8 @@ class Base {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if(count($uniqueWhere) > 0 || count($uniqueOr) > 0) {
|
|
|
|
- $exist = $this->db()->limit(1)->where($uniqueWhere)->orTerms($uniqueOr)->selectRow($this->getTable());
|
|
|
|
|
|
+ if(count($uniqueWhere) > 0) {
|
|
|
|
+ $exist = $this->db()->limit(1)->where($uniqueWhere)->selectRow($this->getTable());
|
|
if($exist) {
|
|
if($exist) {
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist]]
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist]]
|
|
@@ -502,7 +506,9 @@ class Base {
|
|
*/
|
|
*/
|
|
public function remove() {
|
|
public function remove() {
|
|
if(!$this->properties()) {
|
|
if(!$this->properties()) {
|
|
- return false;
|
|
|
|
|
|
+ return Response::FailRemove(static::class .'::'. __FUNCTION__,
|
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => '未指定删除属性,不允许删除', 'body' => 0]
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
$properties = [];
|
|
$properties = [];
|
|
foreach ($this->properties() as $key => $val) {
|
|
foreach ($this->properties() as $key => $val) {
|
|
@@ -515,8 +521,8 @@ class Base {
|
|
}
|
|
}
|
|
$affectedRows = $this->db()->where($this->getWhereHooker())->delete($this->getTable());
|
|
$affectedRows = $this->db()->where($this->getWhereHooker())->delete($this->getTable());
|
|
if($this->db()->isError()) {
|
|
if($this->db()->isError()) {
|
|
- return Response::FailSave(static::class .'::'. __FUNCTION__,
|
|
|
|
- ['_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []]
|
|
|
|
|
|
+ return Response::FailRemove(static::class .'::'. __FUNCTION__,
|
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => 0]
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
return Response::Success(static::class .'::'. __FUNCTION__,
|
|
return Response::Success(static::class .'::'. __FUNCTION__,
|
|
@@ -526,46 +532,42 @@ class Base {
|
|
/**
|
|
/**
|
|
* 更新
|
|
* 更新
|
|
*
|
|
*
|
|
- * @return mixed | Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed | Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function update(){
|
|
|
|
|
|
+ final public function update(){
|
|
$valid = $this->validFieldsForUpdate();
|
|
$valid = $this->validFieldsForUpdate();
|
|
if($valid->isError()) {
|
|
if($valid->isError()) {
|
|
return $valid;
|
|
return $valid;
|
|
}
|
|
}
|
|
//检查是否有重复的
|
|
//检查是否有重复的
|
|
- list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
|
|
|
|
|
|
+ list($uniqueWhere, $exclude, $primaryKey) = $this->condition();
|
|
// 检查 unique 是否已经存在相关数据
|
|
// 检查 unique 是否已经存在相关数据
|
|
$diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
|
|
$diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
|
|
- $diffOr = array_diff_assoc($uniqueOr, $primaryKey);
|
|
|
|
- /*print_r(
|
|
|
|
- [
|
|
|
|
- 'where' => $uniqueWhere,
|
|
|
|
- 'or' => $uniqueOr,
|
|
|
|
- 'exclude' => $exclude,
|
|
|
|
- 'primary' => $primaryKey
|
|
|
|
- ]
|
|
|
|
- );*/
|
|
|
|
- /*$diffExclude = array_diff_assoc($exclude, $primaryKey);
|
|
|
|
-
|
|
|
|
- print_r(
|
|
|
|
- [
|
|
|
|
- 'diffWhere' => $diffWhere,
|
|
|
|
- 'diffOr' => $diffOr,
|
|
|
|
- 'diffExclude' => $diffExclude
|
|
|
|
- ]
|
|
|
|
- );*/
|
|
|
|
-
|
|
|
|
- if(count($diffWhere) > 0 || count($diffOr) > 0) {
|
|
|
|
- $unique = $this->db()->limit(1)->where($diffWhere)->orTerms($diffOr)->exclude($exclude)->selectRow($this->getTable());
|
|
|
|
|
|
+ if(count($diffWhere) > 0) {
|
|
|
|
+ $unique = $this->db()->limit(1)->where($diffWhere)->exclude($exclude)->selectRow($this->getTable());
|
|
if($unique) {
|
|
if($unique) {
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
);
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ /*$whereHooker = $this->getWhereHooker();
|
|
|
|
+ if(!empty($whereHooker)) {
|
|
|
|
+ $primaryKey = $whereHooker;
|
|
|
|
+ }*/
|
|
|
|
+ $where = [];
|
|
|
|
+ $whereHooker = $this->getWhereHooker();
|
|
|
|
+ foreach ($whereHooker as $key => $val) {
|
|
|
|
+ $keyArr = explode(':', $key);
|
|
|
|
+ if(isset($primaryKey[$keyArr[0]])) {
|
|
|
|
+ unset($primaryKey[$keyArr[0]]);
|
|
|
|
+ $where[$key] = $val;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(count($where) > 0) {
|
|
|
|
+ $primaryKey = $where;
|
|
|
|
+ }
|
|
//检查更新的数据是否存在,以主键为主
|
|
//检查更新的数据是否存在,以主键为主
|
|
$exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
$exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
if($exit === null || $exit === false) {
|
|
if($exit === null || $exit === false) {
|
|
@@ -591,46 +593,46 @@ class Base {
|
|
/**
|
|
/**
|
|
* 更新字段
|
|
* 更新字段
|
|
*
|
|
*
|
|
- * @return mixed|\Qii\Driver\Qii\Driver\Response|Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function updateFields() {
|
|
|
|
|
|
+ final public function updateFields() {
|
|
$properties = $this->properties();
|
|
$properties = $this->properties();
|
|
$fields = $this->entity()->convertToProperties(array_keys($properties));
|
|
$fields = $this->entity()->convertToProperties(array_keys($properties));
|
|
$valid = $this->valid($fields);
|
|
$valid = $this->valid($fields);
|
|
if($valid->isError()) {
|
|
if($valid->isError()) {
|
|
return $valid;
|
|
return $valid;
|
|
}
|
|
}
|
|
- list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
|
|
|
|
- /*print_r(
|
|
|
|
- [
|
|
|
|
- 'where' => $uniqueWhere,
|
|
|
|
- 'or' => $uniqueOr,
|
|
|
|
- 'exclude' => $exclude,
|
|
|
|
- 'primary' => $primaryKey
|
|
|
|
- ]
|
|
|
|
- );*/
|
|
|
|
|
|
+ list($uniqueWhere, $exclude, $primaryKey) = $this->condition();
|
|
// 检查 unique 是否已经存在相关数据
|
|
// 检查 unique 是否已经存在相关数据
|
|
$diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
|
|
$diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
|
|
- $diffOr = array_diff_assoc($uniqueOr, $primaryKey);
|
|
|
|
- /*$diffExclude = array_diff_assoc($exclude, $primaryKey);
|
|
|
|
-
|
|
|
|
- print_r(
|
|
|
|
- [
|
|
|
|
- 'diffWhere' => $diffWhere,
|
|
|
|
- 'diffOr' => $diffOr,
|
|
|
|
- 'diffExclude' => $diffExclude
|
|
|
|
- ]
|
|
|
|
- );*/
|
|
|
|
|
|
|
|
- if(count($diffWhere) > 0 || count($diffOr) > 0) {
|
|
|
|
- $unique = $this->db()->limit(1)->where($diffWhere)->orTerms($diffOr)->exclude($exclude)->selectRow($this->getTable());
|
|
|
|
|
|
+ if(count($diffWhere) > 0) {
|
|
|
|
+ $unique = $this->db()->limit(1)->where($diffWhere)->exclude($exclude)->selectRow($this->getTable());
|
|
if($unique) {
|
|
if($unique) {
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
);
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /*$whereHooker = $this->getWhereHooker();
|
|
|
|
+ if(!empty($whereHooker)) {
|
|
|
|
+ $primaryKey = $whereHooker;
|
|
|
|
+ }*/
|
|
|
|
+ $where = [];
|
|
|
|
+ $whereHooker = $this->getWhereHooker();
|
|
|
|
+ foreach ($whereHooker as $key => $val) {
|
|
|
|
+ $keyArr = explode(':', $key);
|
|
|
|
+ if(isset($primaryKey[$keyArr[0]])) {
|
|
|
|
+ unset($primaryKey[$keyArr[0]]);
|
|
|
|
+ $where[$key] = $val;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(count($where) > 0) {
|
|
|
|
+ $primaryKey = $where;
|
|
|
|
+ }
|
|
|
|
+
|
|
//检查更新的数据是否存在,以主键为主
|
|
//检查更新的数据是否存在,以主键为主
|
|
$exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
$exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
if($exit === null || $exit === false) {
|
|
if($exit === null || $exit === false) {
|
|
@@ -657,12 +659,12 @@ class Base {
|
|
/**
|
|
/**
|
|
* 增加或减少某一个字段的值
|
|
* 增加或减少某一个字段的值
|
|
*
|
|
*
|
|
- * @return mixed|Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function incr() {
|
|
|
|
- list($where, $or, $exclude, $primary) = $this->condition();
|
|
|
|
- unset($where, $or, $exclude);
|
|
|
|
|
|
+ final public function incr() {
|
|
|
|
+ list($where, $exclude, $primary) = $this->condition();
|
|
|
|
+ unset($where, $exclude);
|
|
$property = $this->properties();
|
|
$property = $this->properties();
|
|
$incr = [];
|
|
$incr = [];
|
|
foreach ($property as $key => $value) {
|
|
foreach ($property as $key => $value) {
|
|
@@ -685,6 +687,10 @@ class Base {
|
|
$sets[$key . ':minus'] = $val * -1;
|
|
$sets[$key . ':minus'] = $val * -1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ $whereHooker = $this->getWhereHooker();
|
|
|
|
+ if(!empty($whereHooker)) {
|
|
|
|
+ $primary = $whereHooker;
|
|
|
|
+ }
|
|
$affectedRows = $this->db()->set($sets)->where($primary)->update($this->getTable());
|
|
$affectedRows = $this->db()->set($sets)->where($primary)->update($this->getTable());
|
|
|
|
|
|
if($this->db()->isError()) {
|
|
if($this->db()->isError()) {
|
|
@@ -700,11 +706,15 @@ class Base {
|
|
/**
|
|
/**
|
|
* 获取第一条数据
|
|
* 获取第一条数据
|
|
*
|
|
*
|
|
- * @return mixed|\Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function first() {
|
|
|
|
|
|
+ final public function first() {
|
|
$orderBy = $this->getOrderBy();
|
|
$orderBy = $this->getOrderBy();
|
|
|
|
+ //未设置order by 就以主键为准
|
|
|
|
+ if(empty($orderBy)) {
|
|
|
|
+ $orderBy = $this->primaryCondition();
|
|
|
|
+ }
|
|
foreach ($orderBy as $key => $value) {
|
|
foreach ($orderBy as $key => $value) {
|
|
$orderBy[$key] = 'ASC';
|
|
$orderBy[$key] = 'ASC';
|
|
}
|
|
}
|
|
@@ -719,11 +729,15 @@ class Base {
|
|
/**
|
|
/**
|
|
* 获取第一条数据
|
|
* 获取第一条数据
|
|
*
|
|
*
|
|
- * @return mixed|\Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function last() {
|
|
|
|
|
|
+ final public function last() {
|
|
$orderBy = $this->getOrderBy();
|
|
$orderBy = $this->getOrderBy();
|
|
|
|
+ //未设置order by 就以主键为准
|
|
|
|
+ if(empty($orderBy)) {
|
|
|
|
+ $orderBy = $this->primaryCondition();
|
|
|
|
+ }
|
|
foreach ($orderBy as $key => $value) {
|
|
foreach ($orderBy as $key => $value) {
|
|
$orderBy[$key] = 'DESC';
|
|
$orderBy[$key] = 'DESC';
|
|
}
|
|
}
|
|
@@ -742,7 +756,7 @@ class Base {
|
|
* @return array
|
|
* @return array
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function lists($page = 1, $pageSize = 20) {
|
|
|
|
|
|
+ final public function lists($page = 1, $pageSize = 20) {
|
|
$count = $this->count();
|
|
$count = $this->count();
|
|
if(!$this->initPages($data, $count, $page, $pageSize)) {
|
|
if(!$this->initPages($data, $count, $page, $pageSize)) {
|
|
return $data;
|
|
return $data;
|
|
@@ -759,15 +773,31 @@ class Base {
|
|
* @return mixed
|
|
* @return mixed
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function listAll() {
|
|
|
|
|
|
+ final public function listAll() {
|
|
|
|
+ return $this->rsCondition()->selectRows($this->getTable());
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 返回指针
|
|
|
|
+ * @return mixed
|
|
|
|
+ * @throws \Exception
|
|
|
|
+ */
|
|
|
|
+ final public function rs() {
|
|
|
|
+ return $this->rsCondition()->rs($this->getTable());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通过此方法获取 rs
|
|
|
|
+ * @return mixed
|
|
|
|
+ */
|
|
|
|
+ private function rsCondition() {
|
|
$limit = $this->getLimitHooker();
|
|
$limit = $this->getLimitHooker();
|
|
if(empty($limit) || !is_array($limit)) {
|
|
if(empty($limit) || !is_array($limit)) {
|
|
- return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy())->selectRows($this->getTable());
|
|
|
|
|
|
+ return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy());
|
|
}
|
|
}
|
|
if(count($limit) == 1) {
|
|
if(count($limit) == 1) {
|
|
- return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy())->limit($limit[0])->selectRows($this->getTable());
|
|
|
|
|
|
+ return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy())->limit($limit[0]);
|
|
}
|
|
}
|
|
- return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy())->limit($limit[0], $limit[1])->selectRows($this->getTable());
|
|
|
|
|
|
+ return $this->db()->fields($this->getFieldsHooker())->where($this->getWhereHooker())->orderBy($this->getOrderBy())->limit($limit[0], $limit[1]);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 验证相关字段
|
|
* 验证相关字段
|
|
@@ -775,7 +805,7 @@ class Base {
|
|
* @param array $fields 字段列表
|
|
* @param array $fields 字段列表
|
|
* @return Response
|
|
* @return Response
|
|
*/
|
|
*/
|
|
- public function valid($fields = array()) {
|
|
|
|
|
|
+ final public function valid($fields = array()) {
|
|
if(!is_array($fields)) {
|
|
if(!is_array($fields)) {
|
|
return Response::FailValidate(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => '字段必须是数组', 'fields' => []]]);
|
|
return Response::FailValidate(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => '字段必须是数组', 'fields' => []]]);
|
|
}
|
|
}
|
|
@@ -810,10 +840,10 @@ class Base {
|
|
/**
|
|
/**
|
|
* 合并多个 valid 结果 $this->>valids($this->valid(['Uid']), $this->valid(['Nickname', 'Email']));
|
|
* 合并多个 valid 结果 $this->>valids($this->valid(['Uid']), $this->valid(['Nickname', 'Email']));
|
|
* @param ...
|
|
* @param ...
|
|
- * @return mixed|Qii\Driver\Response
|
|
|
|
|
|
+ * @return mixed|Response
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function valids() {
|
|
|
|
|
|
+ final public function valids() {
|
|
$validArr = func_get_args();
|
|
$validArr = func_get_args();
|
|
if(count($validArr) == 0) {
|
|
if(count($validArr) == 0) {
|
|
return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
|
|
return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
|
|
@@ -850,28 +880,37 @@ class Base {
|
|
*/
|
|
*/
|
|
protected function uniqueCondition() {
|
|
protected function uniqueCondition() {
|
|
$uniqueWhere = [];
|
|
$uniqueWhere = [];
|
|
- $uniqueOr = [];
|
|
|
|
$unique = $this->uniqueKey();
|
|
$unique = $this->uniqueKey();
|
|
$useWhere = true;
|
|
$useWhere = true;
|
|
-
|
|
|
|
|
|
+ $condition = [];
|
|
|
|
+ if(empty($unique)) {
|
|
|
|
+ return $condition;
|
|
|
|
+ }
|
|
if(!is_array($unique)) {
|
|
if(!is_array($unique)) {
|
|
$unique = $this->explode(',', $unique);
|
|
$unique = $this->explode(',', $unique);
|
|
- $useWhere = false;
|
|
|
|
- if(count($unique) == 1) {
|
|
|
|
- $useWhere = true;
|
|
|
|
|
|
+ foreach ($unique as $val) {
|
|
|
|
+ $condition[] = $val;
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- foreach ($unique as $key) {
|
|
|
|
- $key = $this->entity()->convertToField($key);
|
|
|
|
- $property = $this->entity()->convertToProperty($key);
|
|
|
|
- if($useWhere) {
|
|
|
|
- $uniqueWhere[$key] = $this->$property;
|
|
|
|
|
|
+ }else{
|
|
|
|
+ //检查是否是一维数组,一维数组联合唯一
|
|
|
|
+ if(count($unique) == count($unique, 1)) {
|
|
|
|
+ $condition[] = $unique;
|
|
}else{
|
|
}else{
|
|
- $uniqueOr[$key] = $this->$property;
|
|
|
|
|
|
+ $condition = $unique;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ foreach ($condition as $item) {
|
|
|
|
+ $uniqueWhere[] = "OR";
|
|
|
|
+ $arr = [];
|
|
|
|
+ foreach ($item as $key) {
|
|
|
|
+ $key = $this->entity()->convertToField($key);
|
|
|
|
+ $property = $this->entity()->convertToProperty($key);
|
|
|
|
+ $arr[$key] = $this->$property;
|
|
}
|
|
}
|
|
|
|
+ $uniqueWhere[] = $arr;
|
|
}
|
|
}
|
|
- return [$uniqueWhere, $uniqueOr];
|
|
|
|
|
|
+ return $uniqueWhere;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -919,13 +958,13 @@ class Base {
|
|
*
|
|
*
|
|
* @return array [$uniqueWhere, $uniqueOr, $excludeCondition, $primaryCondition]
|
|
* @return array [$uniqueWhere, $uniqueOr, $excludeCondition, $primaryCondition]
|
|
*/
|
|
*/
|
|
- public function condition() {
|
|
|
|
|
|
+ final public function condition() {
|
|
//如果设置了 unique 就先验证唯一性,保存的时候验证 uniqueKey,更新的时候验证uniqueKey并排primaryKey
|
|
//如果设置了 unique 就先验证唯一性,保存的时候验证 uniqueKey,更新的时候验证uniqueKey并排primaryKey
|
|
//保存数据的时候验证uniqueKey;更新时验证 uniqueKey,并且排除 primaryKey,如果uniqueKey == primaryKey则不去做唯一性验证
|
|
//保存数据的时候验证uniqueKey;更新时验证 uniqueKey,并且排除 primaryKey,如果uniqueKey == primaryKey则不去做唯一性验证
|
|
- list($uniqueWhere, $uniqueOr) = $this->uniqueCondition();
|
|
|
|
|
|
+ $uniqueWhere = $this->uniqueCondition();
|
|
$excludeCondition = $this->excludeCondition();
|
|
$excludeCondition = $this->excludeCondition();
|
|
$primaryCondition = $this->primaryCondition();
|
|
$primaryCondition = $this->primaryCondition();
|
|
- return [$uniqueWhere, $uniqueOr, $excludeCondition, $primaryCondition];
|
|
|
|
|
|
+ return [$uniqueWhere, $excludeCondition, $primaryCondition];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -933,11 +972,11 @@ class Base {
|
|
*
|
|
*
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public function getOrderBy() {
|
|
|
|
|
|
+ final public function getOrderBy() {
|
|
$order = array();
|
|
$order = array();
|
|
- $orderBy = $this->orderBy();
|
|
|
|
|
|
+ $orderBy = (array) $this->orderBy();
|
|
if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'])) {
|
|
if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'])) {
|
|
- $orderBy = call_user_func($this->hooker['order']['func'], $order, $this->hooker['order']['args']);
|
|
|
|
|
|
+ $orderBy = (array) call_user_func($this->hooker['order']['func'], $order, $this->hooker['order']['args']);
|
|
}
|
|
}
|
|
foreach ($orderBy as $key => $val) {
|
|
foreach ($orderBy as $key => $val) {
|
|
$field = $this->entity()->convertToField($key);
|
|
$field = $this->entity()->convertToField($key);
|
|
@@ -947,7 +986,7 @@ class Base {
|
|
}
|
|
}
|
|
$order[$field] = $val;
|
|
$order[$field] = $val;
|
|
}
|
|
}
|
|
- return $order;
|
|
|
|
|
|
+ return (array) $order;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1001,7 +1040,7 @@ class Base {
|
|
*
|
|
*
|
|
* @return mixed|\Qii\Driver\Qii\Driver\Response
|
|
* @return mixed|\Qii\Driver\Qii\Driver\Response
|
|
*/
|
|
*/
|
|
- public function response() {
|
|
|
|
|
|
+ final public function response() {
|
|
$properties = $this->properties();
|
|
$properties = $this->properties();
|
|
if(!$properties) {
|
|
if(!$properties) {
|
|
return Response::Fail(static::class .'::'. __FUNCTION__,
|
|
return Response::Fail(static::class .'::'. __FUNCTION__,
|
|
@@ -1050,7 +1089,7 @@ class Base {
|
|
(isset($policy['life_time']) ? $policy['life_time'] : $this->defaultExpired);
|
|
(isset($policy['life_time']) ? $policy['life_time'] : $this->defaultExpired);
|
|
$res = call_user_func_array(array($this, $method), $args);
|
|
$res = call_user_func_array(array($this, $method), $args);
|
|
$str = serialize($res);
|
|
$str = serialize($res);
|
|
- $func->set($key, $str, ['life_time' => $config['life_time']]);
|
|
|
|
|
|
+ $func->set($key, $str, ['life_time' => $expiredAt]);
|
|
return $res;
|
|
return $res;
|
|
}
|
|
}
|
|
}
|
|
}
|