|
@@ -0,0 +1,722 @@
|
|
|
+<?php
|
|
|
+namespace Qii\Driver\Entity;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+use Qii\Driver\Response;
|
|
|
+
|
|
|
+class Base {
|
|
|
+ public static $propertys;
|
|
|
+ public function __construct(){
|
|
|
+ }
|
|
|
+ public function defaultFieldsValue(){}
|
|
|
+ public function getTable() {
|
|
|
+ throw new \Exception('请先设置需要保存的数据表');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回使用的db信息
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function db() {
|
|
|
+ return _loadClass('\Qii\Driver\Model');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回 entity 信息
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function entity() {
|
|
|
+ return _loadClass('\Qii\Driver\Entity\Entity');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unique (unique 如果是 array 则表示 联合唯一,如果是string则是任意唯一,多个单独唯一使用字符串以逗号隔开, 主键除外)
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function uniqueKey(){
|
|
|
+ throw new \Exception('请设置唯一值');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主键
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function primaryKey(){
|
|
|
+ throw new \Exception('请设置主键');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个排除
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function exclude(){
|
|
|
+ return array();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * order by
|
|
|
+ * @return string[]
|
|
|
+ */
|
|
|
+ public function orderBy() {
|
|
|
+ return array();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * explode string
|
|
|
+ * @param string $separator 分隔符
|
|
|
+ * @param string $string 分割的字符串
|
|
|
+ * @param int $limit 是否
|
|
|
+ * @return array|false|string[]
|
|
|
+ */
|
|
|
+ public function explode($separator, $string, $limit = PHP_INT_MAX) {
|
|
|
+ if($string == '') {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ return explode($separator, $string, $limit);
|
|
|
+ }
|
|
|
+ public function validFieldsForAdd(){
|
|
|
+ throw new \Exception('请设置插入数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function validFieldsForUpdate() {
|
|
|
+ throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取不为空的属性
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function properties() {
|
|
|
+ $class = get_called_class();
|
|
|
+ $method = new \ReflectionClass($class);
|
|
|
+ $properties = $method->getproperties();
|
|
|
+ $fields = [];
|
|
|
+ foreach($properties as $property) {
|
|
|
+ if($property->isPublic()) {
|
|
|
+ $name = $property->getName();
|
|
|
+ if(!isset($this->$name)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $field = $this->entity()->convertToField($name);
|
|
|
+ $fields[$field] = $this->$name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $fields;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取默认值
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDefaultValue() {
|
|
|
+ $default = [];
|
|
|
+ $defaultValue = $this->defaultFieldsValue();
|
|
|
+ if(!$defaultValue || !is_array($defaultValue) || count($defaultValue) == 0) {
|
|
|
+ return $default;
|
|
|
+ }
|
|
|
+ foreach ($defaultValue as $key => $value) {
|
|
|
+ $field = $this->entity()->convertToField($key);
|
|
|
+ $default[$field] = $value;
|
|
|
+ }
|
|
|
+ return $default;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取总行数
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function count() {
|
|
|
+ return $this->db()->fields(' COUNT(1) as count')->where($this->properties())->selectOne($this->getTable());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查是否有对应的属性
|
|
|
+ *
|
|
|
+ * @param $key
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function hasProperty($key) {
|
|
|
+ $class = get_called_class();
|
|
|
+ $key = $this->entity()->convertToProperty($key);
|
|
|
+ if(isset(self::$propertys[$class])) {
|
|
|
+ return isset(self::$propertys[$class][$key]);
|
|
|
+ }
|
|
|
+ $method = new \ReflectionClass($class);
|
|
|
+ $properties = $method->getproperties();
|
|
|
+ $fields = [];
|
|
|
+ foreach($properties as $property) {
|
|
|
+ if($property->isPublic()) {
|
|
|
+ $name = $property->getName();
|
|
|
+ $fields[$name] = '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self::$propertys[$class] = $fields;
|
|
|
+ return isset($fields[$key]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * bind value
|
|
|
+ * @param $values
|
|
|
+ * @return $this|false
|
|
|
+ */
|
|
|
+ public function bindValues($values) {
|
|
|
+ if(!is_array($values)) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ foreach ($values as $key => $value) {
|
|
|
+ $property = $this->entity()->convertToProperty($key);
|
|
|
+ if($this->hasProperty($key)) {
|
|
|
+ $this->$property = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function get() {
|
|
|
+ $info = $this->db()->where($this->properties())->selectRow($this->getTable());
|
|
|
+ $class = get_called_class();
|
|
|
+ $obj = new $class();
|
|
|
+ if(!$info) {
|
|
|
+ return $obj;
|
|
|
+ }
|
|
|
+ foreach ($info as $key => $val) {
|
|
|
+ $key = $this->entity()->convertToProperty($key);
|
|
|
+ $obj->$key = $val;
|
|
|
+ }
|
|
|
+ return $obj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否相关数据存在
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function exist() {
|
|
|
+ if(!$this->properties()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return (bool) $this->db()->where($this->properties())->selectOne($this->getTable());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ *
|
|
|
+ * @return mixed|Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function add() {
|
|
|
+ $valid = $this->validFieldsForAdd();
|
|
|
+ if($valid->isError()) {
|
|
|
+ return $valid;
|
|
|
+ }
|
|
|
+ //如果设置了 unique 就先验证唯一性
|
|
|
+ list($uniqueWhere, $uniqueOr, $exclude, $primary) = $this->condition();
|
|
|
+ unset($exclude);
|
|
|
+
|
|
|
+ //如果 $where $or 为空,看看主键是否有设置值,有设置值说明验证主键就可以,反之设置了主键,主键值设置了,只验证主键就可以了
|
|
|
+ if(count($primary) > 0) {
|
|
|
+ $exist = $this->db()->limit(1)->where($primary)->selectRow($this->getTable());
|
|
|
+ if($exist) {
|
|
|
+ return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(count($uniqueWhere) > 0 || count($uniqueOr) > 0) {
|
|
|
+ $exist = $this->db()->limit(1)->where($uniqueWhere)->orTerms($uniqueOr)->selectRow($this->getTable());
|
|
|
+ if($exist) {
|
|
|
+ return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
+ $res = $this->db()->insertObject($this->getTable(), $values);
|
|
|
+
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::FailSave(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => '添加成功', 'body' => $res]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 更新
|
|
|
+ *
|
|
|
+ * @return mixed | Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function update(){
|
|
|
+ $valid = $this->validFieldsForUpdate();
|
|
|
+ if($valid->isError()) {
|
|
|
+ return $valid;
|
|
|
+ }
|
|
|
+ return $this->updateFields();
|
|
|
+ //检查是否有重复的
|
|
|
+ list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
|
|
|
+ // 检查 unique 是否已经存在相关数据
|
|
|
+ $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($unique) {
|
|
|
+ return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查更新的数据是否存在,以主键为主
|
|
|
+ $exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
|
+ if($exit === null || $exit === false) {
|
|
|
+ return Response::NotExist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_NOT_EXIST, 'msg' => '未找到相关记录', 'body' => []]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ //获取默认值
|
|
|
+
|
|
|
+ $values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
+
|
|
|
+ $affectedRows = $this->db()->updateObject($this->getTable(), $values, $primaryKey);
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::FailUpdate(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_UPDATE, 'msg' => $this->db()->getMessage(), 'body' => []]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => '数据更新成功', 'body' => $affectedRows]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function updateFields() {
|
|
|
+ $properties = $this->properties();
|
|
|
+ $fields = $this->entity()->convertToProperties(array_keys($properties));
|
|
|
+ $valid = $this->valid($fields);
|
|
|
+ if($valid->isError()) {
|
|
|
+ return $valid;
|
|
|
+ }
|
|
|
+ list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
|
|
|
+ /*print_r(
|
|
|
+ [
|
|
|
+ 'where' => $uniqueWhere,
|
|
|
+ 'or' => $uniqueOr,
|
|
|
+ 'exclude' => $exclude,
|
|
|
+ 'primary' => $primaryKey
|
|
|
+ ]
|
|
|
+ );*/
|
|
|
+ // 检查 unique 是否已经存在相关数据
|
|
|
+ $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($unique) {
|
|
|
+ return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_EXIST, 'msg' => '已经存在相关记录', 'body' => $unique]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查更新的数据是否存在,以主键为主
|
|
|
+ $exit = $this->db()->limit(1)->where($primaryKey)->selectOne($this->getTable());
|
|
|
+ if($exit === null || $exit === false) {
|
|
|
+ return Response::NotExist(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DOES_NOT_EXIST, 'msg' => '未找到相关记录', 'body' => []]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ //获取默认值
|
|
|
+
|
|
|
+ $values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
+
|
|
|
+ $affectedRows = $this->db()->updateObject($this->getTable(), $values, $primaryKey);
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::FailUpdate(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_UPDATE, 'msg' => $this->db()->getMessage(), 'body' => []]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => '数据更新成功', 'body' => $affectedRows]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加或减少某一个字段的值
|
|
|
+ *
|
|
|
+ * @return mixed|Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function incr() {
|
|
|
+ list($where, $or, $exclude, $primary) = $this->condition();
|
|
|
+ unset($where, $or, $exclude);
|
|
|
+ $property = $this->properties();
|
|
|
+ $incr = [];
|
|
|
+ foreach ($property as $key => $value) {
|
|
|
+ if(!is_numeric($value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $incr[$key] = $value;
|
|
|
+ }
|
|
|
+ $diff = array_diff_assoc($incr, $primary);
|
|
|
+ if(count($diff) == 0) {
|
|
|
+ return Response::Fail(self::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::DO_FAIL, 'msg' => 'INCR 参数错误']]);
|
|
|
+ }
|
|
|
+ $sets = [];
|
|
|
+ foreach ($diff as $key => $val) {
|
|
|
+ if($val == 0) {
|
|
|
+ $sets[$key] = $val;
|
|
|
+ }else if($val > 0) {
|
|
|
+ $sets[$key . ':plus'] = $val;
|
|
|
+ }else if($val < 0) {
|
|
|
+ $sets[$key . ':minus'] = $val * -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $affectedRows = $this->db()->set($sets)->where($primary)->update($this->getTable());
|
|
|
+
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::FailUpdate(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::FAIL_FOR_UPDATE, 'msg' => $this->db()->getMessage(), 'body' => []]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => 'INCR 成功', 'body' => $affectedRows]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取第一条数据
|
|
|
+ *
|
|
|
+ * @return mixed|\Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function first() {
|
|
|
+ $orderBy = $this->getOrderBy();
|
|
|
+ foreach ($orderBy as $key => $value) {
|
|
|
+ $orderBy[$key] = 'ASC';
|
|
|
+ }
|
|
|
+ $row = $this->db()->orderBy($orderBy)->limit(1)->selectRow($this->getTable());
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::Fail(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_SELECT, 'msg' => 'Query 失败', 'body' => []]]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => 'Query 成功', 'body' => $row]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取第一条数据
|
|
|
+ *
|
|
|
+ * @return mixed|\Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function last() {
|
|
|
+ $orderBy = $this->getOrderBy();
|
|
|
+ foreach ($orderBy as $key => $value) {
|
|
|
+ $orderBy[$key] = 'DESC';
|
|
|
+ }
|
|
|
+ $row = $this->db()->orderBy($orderBy)->limit(1)->selectRow($this->getTable());
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::Fail(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_SELECT, 'msg' => 'Query 失败', 'body' => []]]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => 'Query 成功', 'body' => $row]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * listts
|
|
|
+ * @param int $page 页码
|
|
|
+ * @param int $pageSize 页大小
|
|
|
+ * @return array
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function lists($page = 1, $pageSize = 20) {
|
|
|
+ $count = $this->count();
|
|
|
+ if(!$this->initPages($data, $count, $page, $pageSize)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ $order = $this->getOrderBy();
|
|
|
+ $data['lists'] = $this->db()->where($this->properties())->orderBy($order)->limit($data['pages']['limitStart'], $pageSize)->selectRows($this->getTable());
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证相关字段
|
|
|
+ *
|
|
|
+ * @param array $fields 字段列表
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function valid($fields = array()) {
|
|
|
+ if(!is_array($fields)) {
|
|
|
+ return Response::FailValidate(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => '字段必须是数组', 'fields' => []]]);
|
|
|
+ }
|
|
|
+ $rules = $this->rules();
|
|
|
+ $result = [];
|
|
|
+ $invalidKey = [];
|
|
|
+ $values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
+ foreach ($rules as $valid => $rule) {
|
|
|
+ foreach ($rule as $val) {
|
|
|
+ $key = $this->entity()->convertToProperty($val[0]);
|
|
|
+ if(!in_array($key, $fields)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $field = $this->entity()->convertToField($key);
|
|
|
+
|
|
|
+ $verify = new Verify($val[0], $values[$field] ?? null, strtolower($valid), $val[1], $val[2] ?? '');
|
|
|
+ $res = $verify->valid();
|
|
|
+ if($res->isError()) {
|
|
|
+ $result[] = $res->getResult() .',获取的是 '. $values[$field];
|
|
|
+ if(!in_array($field, $invalidKey)) {
|
|
|
+ $invalidKey[] = $field;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(count($result) > 0) {
|
|
|
+ return Response::FailValidate(static::class .'::'. __FUNCTION__, ['_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => $result, 'fields' => $invalidKey]]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并多个 valid 结果 $this->>valids($this->valid(['Uid']), $this->valid(['Nickname', 'Email']));
|
|
|
+ * @param ...
|
|
|
+ * @return mixed|Qii\Driver\Response
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function valids() {
|
|
|
+ $validArr = func_get_args();
|
|
|
+ if(count($validArr) == 0) {
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
|
|
|
+ }
|
|
|
+ if(count($validArr) == 1) {
|
|
|
+ return $validArr[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ $invalid = array();
|
|
|
+ $invalid['message'] = array();
|
|
|
+ $invalid['fields'] = array();
|
|
|
+
|
|
|
+ foreach ($validArr as $valid) {
|
|
|
+ if(!($valid instanceof Response)) {
|
|
|
+ throw new \Exception('验证结果类型必须是\Qii\Drive\Response类型');
|
|
|
+ }
|
|
|
+ if($valid->isError()) {
|
|
|
+ $result = $valid->getResult();
|
|
|
+ $invalid['message'] = array_merge($invalid['message'], $result['message']);
|
|
|
+ $invalid['fields'] = array_merge($invalid['fields'], $result['fields']);;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(count($invalid['message']) > 0) {
|
|
|
+ return Response::Fail(static::class .'::'. __FUNCTION__, ['_result' => ['message' => $invalid['message'], 'fields' => $invalid['fields']]]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unique 条件
|
|
|
+ *
|
|
|
+ * @return array[]
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ protected function uniqueCondition() {
|
|
|
+ $uniqueWhere = [];
|
|
|
+ $uniqueOr = [];
|
|
|
+ $unique = $this->uniqueKey();
|
|
|
+ $useWhere = true;
|
|
|
+
|
|
|
+ if(!is_array($unique)) {
|
|
|
+ $unique = $this->explode(',', $unique);
|
|
|
+ $useWhere = false;
|
|
|
+ if(count($unique) == 1) {
|
|
|
+ $useWhere = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($unique as $key) {
|
|
|
+ $key = $this->entity()->convertToField($key);
|
|
|
+ $property = $this->entity()->convertToProperty($key);
|
|
|
+ if($useWhere) {
|
|
|
+ $uniqueWhere[$key] = $this->$property;
|
|
|
+ }else{
|
|
|
+ $uniqueOr[$key] = $this->$property;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [$uniqueWhere, $uniqueOr];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * exclude Condition
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected function excludeCondition() {
|
|
|
+ if(is_array($this->exclude())) {
|
|
|
+ $exclude = $this->exclude();
|
|
|
+ }else{
|
|
|
+ $exclude = (array) $this->explode(',', $this->exclude());
|
|
|
+ }
|
|
|
+ $excludeCondition = [];
|
|
|
+ foreach ($exclude as $key) {
|
|
|
+ $field = $this->entity()->convertToField($key);
|
|
|
+ $property = $this->entity()->convertToProperty($key);
|
|
|
+ $excludeCondition[$field] = $this->$property;
|
|
|
+ }
|
|
|
+ return $excludeCondition;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主键
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ protected function primaryCondition(){
|
|
|
+ $primary = array();
|
|
|
+ $primaryKey = $this->primaryKey();
|
|
|
+ if(!is_array($primaryKey)) {
|
|
|
+ $primaryKey = $this->explode(',', $primaryKey);
|
|
|
+ }
|
|
|
+ foreach ($primaryKey as $key) {
|
|
|
+ $key = $this->entity()->convertToProperty($key);
|
|
|
+ $field = $this->entity()->convertToField($key);
|
|
|
+ $value = $this->$key;
|
|
|
+ $primary[$field] = $value;
|
|
|
+ }
|
|
|
+ return $primary;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取查询条件
|
|
|
+ *
|
|
|
+ * @return array [$uniqueWhere, $uniqueOr, $excludeCondition, $primaryCondition]
|
|
|
+ */
|
|
|
+ public function condition() {
|
|
|
+ //如果设置了 unique 就先验证唯一性,保存的时候验证 uniqueKey,更新的时候验证uniqueKey并排primaryKey
|
|
|
+ //保存数据的时候验证uniqueKey;更新时验证 uniqueKey,并且排除 primaryKey,如果uniqueKey == primaryKey则不去做唯一性验证
|
|
|
+ list($uniqueWhere, $uniqueOr) = $this->uniqueCondition();
|
|
|
+ $excludeCondition = $this->excludeCondition();
|
|
|
+ $primaryCondition = $this->primaryCondition();
|
|
|
+ return [$uniqueWhere, $uniqueOr, $excludeCondition, $primaryCondition];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取order by
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getOrderBy() {
|
|
|
+ $order = array();
|
|
|
+ $orderBy = $this->orderBy();
|
|
|
+ foreach ($orderBy as $key => $val) {
|
|
|
+ $field = $this->entity()->convertToField($key);
|
|
|
+ $val = strtoupper($val);
|
|
|
+ if(!in_array($val, array('DESC', 'ASC'))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $order[$field] = $val;
|
|
|
+ }
|
|
|
+ return $order;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化page
|
|
|
+ * @param array $data 返回的数据
|
|
|
+ * @param int $count 总数量
|
|
|
+ * @param int $page 页码
|
|
|
+ * @param int $pageSize 显示数量
|
|
|
+ * @param int $pagination 分页显示最多页码
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function initPages(&$data, $count, $page, $pageSize = 20, $pagination = 5) {
|
|
|
+ $page = max(1, $page);
|
|
|
+ $pageSize = max(1, $pageSize);
|
|
|
+ $data['start'] = 0;
|
|
|
+ $data['pages'] = array('total' => 0, 'currentPage' => 0, 'totalPage' => 0);
|
|
|
+ $data['pages']['total'] = (int) ($count ? $count : 0);
|
|
|
+ $data['pages']['currentPage'] = $page;
|
|
|
+ $data['pages']['totalPage'] = ceil($data['pages']['total'] / $pageSize);
|
|
|
+ $data['lists'] = array();
|
|
|
+ if ($data['pages']['currentPage'] > $data['pages']['totalPage']) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $frames = array(
|
|
|
+ 'left' => 0,
|
|
|
+ 'right' => 0
|
|
|
+ );
|
|
|
+ $pagination = $pagination < $data['pages']['totalPage'] ? $pagination : $data['pages']['totalPage'];
|
|
|
+
|
|
|
+ if($data['pages']['currentPage'] - floor($pagination / 2) <= 0) {
|
|
|
+ $frames['left'] = 1;
|
|
|
+ $frames['right'] = $frames['left'] + $pagination - 1;
|
|
|
+ } else if($data['pages']['currentPage'] + floor($pagination / 2) >= $data['pages']['totalPage']) {
|
|
|
+ $frames['right'] = $data['pages']['totalPage'];
|
|
|
+ $frames['left'] = $frames['right'] - $pagination + 1;
|
|
|
+ } else {
|
|
|
+ $frames['left'] = $data['pages']['currentPage'] - floor($pagination / 2);
|
|
|
+ $frames['right'] = $data['pages']['currentPage'] + ceil($pagination / 2) - 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $data['start'] = $frames['left'];
|
|
|
+ $data['pages']['start'] = $data['start'];
|
|
|
+ $data['pages']['end'] = $frames['right'];
|
|
|
+ $data['pages']['pagination'] = $pagination;
|
|
|
+ $data['pages']['limitStart'] = (min($page, $data['pages']['totalPage']) - 1) * $pageSize;
|
|
|
+ $data['pages']['pageSize'] = (int) $pageSize;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认转发到 db model上
|
|
|
+ * @param string $method method
|
|
|
+ * @param array $args
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function __call($method, $args) {
|
|
|
+ return call_user_func_array(array($this->db(), $method), $args);
|
|
|
+ }
|
|
|
+}
|