Jelajahi Sumber

Update : 数据库操作类

朱金辉 2 tahun lalu
induk
melakukan
c39a8ba5d6

+ 3 - 2
src/Base/Rules.php

@@ -31,7 +31,8 @@ use Qii\Execptions;
  * @method bool addStringFields(array $data = [string, string, bool|string|int], array $data=[...])  [string $field string $msg string $extparam],...
  * @method bool addSetsFields(array $data = [string, string, bool|string|int], array $data=[...])  [string $field string $msg string $extparam],...
  * @method bool addSetsArrayFields(array $data = [string, string, bool|string|int], array $data=[...])  [string $field string $msg string $extparam],...
- *
+ * @method bool addDecimalFields(array $data = [string, string, string], array $data=[...]) [string $field string $msg string $extparam],...
+ * @method bool addFloatFields(array $data = [string, string], array $data=[...]) [string $field string $msg string $extparam],...
  */
 class Rules 
 {
@@ -460,7 +461,7 @@ class Rules
             'qq', 'postcode', 'ip', 'phone', 'telephone',
             'mobile', 'en', 'cn', 'account', 'number', 'date',
             'safe', 'password', 'maxlength', 'minlength', 'length',
-            'rangeof', 'string', 'sets', 'setsarray'
+            'rangeof', 'string', 'sets', 'setsarray', 'float', 'decimal'
         );
         return in_array($key, $allow);
     }

+ 7 - 8
src/Driver/Base.php

@@ -1,9 +1,8 @@
 <?php
-
 namespace Qii\Driver;
 use Qii\Autoloader\Psr4;
+use Qii\Exceptions\InvalidFormat;
 use Qii\Exceptions\InvalidParams;
-use Qii\Exceptions\MethodNotFound;
 use Qii\Exceptions\TableException;
 
 /**
@@ -32,6 +31,7 @@ use Qii\Exceptions\TableException;
  *           $this->db->join(array("leftJoin", array("table" => 'table', 'alias' => 'a', 'on' => 'a.id=b.id')));
  *           $this->db->join(" LEFT JOIN table c on c.id=a.id")->selectAll('use a');
  *           $this->db->where(array('email:unequal' => 'test@test.com'))->in(array('uid:in' => array('1,2,3'), 'status' => array(1)))->selectAll('user');
+ *           $this->>db->where(array('uid:in' => '1,2,3', 'email' => 'test@tset.com', 'status' => 1))->selectAll('user');
  *           $this->db->where(array(array('email:unequal' => 'test@test.com'), array('uid:in' => array('1,2,3'), 'status:in' => array(1))))->selectAll('user');
  *      }
  * }
@@ -145,7 +145,7 @@ class Base
      * @param $table
      * @param $dataArray
      * @return int
-     * @throws \Qii\Exceptions\InvalidFormat
+     * @throws InvalidFormat
      */
     final function insertObject($table, $dataArray)
     {
@@ -189,7 +189,7 @@ class Base
      *
      * @param $dataArray
      * @return array
-     * @throws \Qii\Exceptions\InvalidFormat
+     * @throws InvalidFormat
      */
     protected function createInsertReplaceObj($dataArray)
     {
@@ -199,7 +199,7 @@ class Base
             foreach ($dataArray AS $key => $value) {
                 $keys[] = $key;
                 if (is_array($value)) {
-                    throw new \Qii\Exceptions\InvalidFormat(_i('Invalid %s format', $key), __LINE__);
+                    throw new InvalidFormat(_i('Invalid %s format', $key), __LINE__);
                 }
                 $values[] = $this->setQuote($value);
             }
@@ -749,7 +749,7 @@ class Base
      * 执行Model过程中保存的相关信息
      *
      * @param String $option
-     * @return Mix
+     * @return mixed
      */
     final function querySQL($option = '')
     {
@@ -913,7 +913,6 @@ class Base
         $tmpWhere = array();
         //最后一次是否是赋值
         $lastIsValue = null;
-        $groupCount = count($group);
         foreach($group as $index => $val)
         {
             $isOperator = $this->isOperator($val);//如果是操作符,上一个不是操作符就清空tmpWhere,并放入slices
@@ -973,7 +972,7 @@ class Base
      * where条件子句组合
      *
      * @param array $values ['字段1' => '值1', '字段2' => '值2', ...]
-     * @param $defaultOperate 默认字句的链接方式
+     * @param string $defaultOperate 默认字句的链接方式
      *  ..... 额外的参数,用于字段与字段之间的连接
      * @return string
      * @throws \Exception

+ 111 - 10
src/Driver/EasyDrive.php

@@ -6,7 +6,7 @@
  *
  * usage:
  *
- * @method setPrivateKey(array $data);
+ *
  */
 namespace Qii\Driver;
 
@@ -66,7 +66,13 @@ use Qii\Exceptions\InvalidParams;
         return $response->getResult();
    }
  *
- * @method exist()
+ * @method EasyORM setQueryFields(array $fields)
+ * @method void cleanQueryFields()
+ * @method void cleanValues()
+ * @method EasyORM setLimit(int $start, int $end)
+ * @method EasyORM cleanLimit()
+ * @method EasyORM  cleanCondition()
+ * @method EasyORM cleanFieldsVal()
  */
 class EasyDrive {
     /**
@@ -91,13 +97,21 @@ class EasyDrive {
     public $_validator;
     private $_message = [];
     /**
+     * @param string $rules 规则类名
      * @throws InvalidParams
      */
-    public function __construct(){
+    public function __construct($rules = ''){
         if ($this->_ormRules == '') {
             throw new InvalidParams('_ormRules is null');
         }
-        $this->_easyORM = new \Qii\Driver\EasyORM(_loadClass($this->_ormRules));
+
+        if($rules != '') {
+            $this->_ormRules = $rules;
+        }
+
+        $rules = new $this->_ormRules();
+        $this->_easyORM = new EasyORM($rules);
+
         $this->db = $this->getDB();
         $this->_validator = _library("Validate");
     }
@@ -118,6 +132,14 @@ class EasyDrive {
     public function verify() {
         return true;
     }
+    /**
+     * 清除数据
+     *
+     * @return EasyORM
+     */
+    final public function clean() {
+        return $this->_easyORM->clean();
+    }
 
     /**
      * 获取DB属性
@@ -235,7 +257,7 @@ class EasyDrive {
      * @param array $array 数组形式
      * @return EasyORM
      */
-    public function setPrivateKey($array) {
+    final public function setPrivateKey($array) {
         $this->_easyORM->setPrivateKey($array);
         return $this;
     }
@@ -246,7 +268,7 @@ class EasyDrive {
      * @param array $args
      * @return $this
      */
-    public function where($args) {
+    final public function where($args) {
         $this->_easyORM->where($args);
         return $this;
     }
@@ -255,7 +277,7 @@ class EasyDrive {
      * 清除 where 条件
      * @return $this
      */
-    public function cleanWhere() {
+    final public function cleanWhere() {
         $this->_easyORM->cleanWhere();
         return $this;
     }
@@ -264,7 +286,7 @@ class EasyDrive {
      *
      * @return $this
      */
-    public function cleanPrivateKey() {
+    final public function cleanPrivateKey() {
         $this->_easyORM->cleanPrivateKey();
         return $this;
     }
@@ -275,7 +297,7 @@ class EasyDrive {
      * @param array $array
      * @return $this
      */
-    public function setExclude($array) {
+    final public function setExclude($array) {
         $this->_easyORM->setExcluded($array);
         return $this;
     }
@@ -285,10 +307,89 @@ class EasyDrive {
      *
      * @return $this
      */
-    public function cleanExclude() {
+    final public function cleanExclude() {
         $this->_easyORM->cleanExcluded();
         return $this;
     }
+    /**
+     * like([['key' => 'value'], ...]->count() like([['key' => 'value'], ...]->lists()
+     * @param $values
+     * @return Qii\Driver\Response
+     */
+    final public function like($values = array()) {
+        return $this->_easyORM->like($values);
+    }
+    /**
+     * 获取指定条件返回数据的行数
+     *
+     * @return Qii\Driver\Response
+     */
+    final public function count() {
+        return $this->_easyORM->count();
+    }
+    /**
+     * update
+     * @return Qii\Driver\Response
+     */
+    final public function update(){
+        return $this->_easyORM->update();
+    }
+    /**
+     * 删除指定数据
+     *
+     * @return Qii\Driver\Response
+     */
+    final public function remove() {
+        return $this->_easyORM->remove();
+    }
+
+    /**
+     * 更新字段,与update的区别是只验更新字段的值,不验证update设定的字段
+     *
+     * @return mixed|Qii\Driver\Response
+     */
+    final public function updateFields(){
+        return $this->_easyORM->updateFields();
+    }
+
+    /**
+     * lists
+     *
+     * @return mixed
+     */
+    final public function lists() {
+        return $this->_easyORM->lists();
+    }
+    /**
+     * 设置order by
+     * @param $orderBy
+     * @return $this
+     */
+    final public function setOrderBy($orderBy) {
+        return $this->_easyORM->setOrderBy($orderBy);
+    }
+    /**
+     * @return Qii\Driver\Response|mixed
+     */
+    final public function save() {
+        return $this->_easyORM->save();
+    }
+
+    /**
+     * incr 某一个字段
+     * @return mixed|Qii\Driver\Response
+     */
+    final public function incr() {
+        return $this->_easyORM->incr();
+    }
+    /**
+     * 检查数据是否已经存在,并返回一行,只能根据主键查询
+     *
+     * @return \Qii\Driver\Response
+     */
+    final public function exist() {
+        return $this->_easyORM->exist();
+    }
     /**
      * __call 自动调用_easyORM中的方法
      *

+ 271 - 72
src/Driver/EasyORM.php

@@ -16,10 +16,10 @@ final class EasyORM {
      * @throws InvalidParams
      */
     public function __construct(\Qii\Base\Rules $rules) {
+        $this->_easyORM = new stdClass();
         $this->clean();
+        $this->cleanCondition();
         $this->_easyORM->db = \Qii::getInstance("\Qii\Driver\Model")->db;
-        $this->_easyORM->queryFields = "*";
-        $this->_easyORM->where = [];
         $this->setRules($rules);
         $this->setFields($rules->fields());
         $this->setResponse(new Response());
@@ -30,7 +30,7 @@ final class EasyORM {
      *
      * @return mixed
      */
-    public function getDB() {
+    final public function getDB() {
         return $this->_easyORM->db;
     }
 
@@ -40,8 +40,9 @@ final class EasyORM {
      * @param \Qii\Base\Rules $rules
      * @return void
      */
-    public function setRules(\Qii\Base\Rules $rules) {
+    final public function setRules(\Qii\Base\Rules $rules) {
         $this->_easyORM->rules = $rules;
+        return $this;
     }
 
     /**
@@ -49,7 +50,7 @@ final class EasyORM {
      *
      * @return mixed
      */
-    public function getRules() {
+    final public function getRules() {
         return $this->_easyORM->rules;
     }
 
@@ -59,8 +60,9 @@ final class EasyORM {
      * @param string $table 表名
      * @return mixed
      */
-    public function setTable($table) {
-        return $this->_easyORM->rules->setTable($table);
+    final public function setTable($table) {
+        $this->_easyORM->rules->setTable($table);
+        return $this;
     }
 
     /**
@@ -68,7 +70,7 @@ final class EasyORM {
      * 
      * @return mixed
      */
-    public function getTable() {
+    final public function getTable() {
         return $this->_easyORM->rules->getTable();
     }
     /**
@@ -78,19 +80,20 @@ final class EasyORM {
      * @return void
      * @throws
      */
-    public function setFields($fields = array()) {
+    final public function setFields($fields = array()) {
         if(!is_array($fields)) {
             throw new InvalidParams("数据表字段不能为空");
         }
         $this->_easyORM->fields = new Fields($fields);
+        return $this;
     }
 
     /**
      * 设置查询的 Fields
-     * @param $fields
+     * @param array|string $fields
      * @return $this
      */
-    public function setQueryFields($fields) {
+    final public function setQueryFields($fields) {
         if(!$fields) return $this;
         if(is_array($fields)) {
             $this->_easyORM->queryFields = join(",", $fields);
@@ -100,15 +103,30 @@ final class EasyORM {
         return $this;
     }
 
+    /**
+     * 清除 fields
+     * @return void
+     */
+    final public function cleanQueryFields() {
+        $this->_easyORM->queryFields = '*';
+        return $this;
+    }
+
     /**
      * @param array $args [[key, $value],...]
      * @return EasyORM
      */
-    public function where($args) {
+    final public function where($args) {
         if(!is_array($args)) {
             return $this;
         }
-        $this->_easyORM->where = $args;
+        $this->_easyORM->where = array();
+        foreach ($args as $key => $val) {
+            $explode = explode(":", $key)[0];
+            if($this->_easyORM->fields->hasField($explode)) {
+                $this->_easyORM->where[$key] = $val;
+            }
+        }
         return $this;
     }
 
@@ -116,10 +134,20 @@ final class EasyORM {
      * 清除where条件
      * @return $this
      */
-    public function cleanWhere() {
+    final public function cleanWhere() {
         $this->_easyORM->where = array();
         return $this;
     }
+
+    /**
+     * 清除like
+     *
+     * @return $this
+     */
+    final public function cleanLike() {
+        $this->_easyORM->like = array();
+        return $this;
+    }
     /**
      * 获取所有字段
      *
@@ -173,7 +201,6 @@ final class EasyORM {
      * @return void
      */
     public function __unset($name) {
-        echo 'unset';
         if(isset($this->_easyORM->fields->$name)) unset($this->_easyORM->fields->$name);
     }
     /**
@@ -199,29 +226,56 @@ final class EasyORM {
      *
      * @return mixed
      */
-    public function getValueAsArray() {
+    final public function getValueAsArray() {
         return $this->_easyORM->fields->getValueAsArray();
     }
     /**
      * @param Response $response
      * @return void
      */
-    public function setResponse(Response $response) {
+    final public function setResponse(Response $response) {
         $this->_easyORM->response = $response;
+        return $response;
     }
 
     /**
      * @return EasyORM
      */
-    protected function clean(){
-        $this->_easyORM = new stdClass();
-        $this->_easyORM->rules = null;
-        $this->_easyORM->db = null;
-        $this->_easyORM->fields = null;
+    final public function clean(){
         $this->_easyORM->limit = array();
         $this->_easyORM->response = null;
         $this->_easyORM->privateKeys = array();
+        $this->_easyORM->where = array();
+        $this->_easyORM->like = array();
         $this->_easyORM->exclude = array();
+        $this->_easyORM->orderBy = array();
+        $this->_easyORM->queryFields = '*';
+        return $this;
+    }
+
+    /**
+     * 清空所有的条件和字段的值
+     *
+     * @return EasyORM
+     */
+    final public function cleanCondition() {
+        $this->cleanPrivateKey();
+        $this->cleanWhere();
+        $this->cleanLike();
+        $this->cleanLimit();
+        $this->cleanExcluded();
+        $this->cleanQueryFields();
+        $this->cleanOrderBy();
+        return $this;
+    }
+
+    /**
+     * 清除所有设置了字段的值
+     *
+     * @return EasyORM
+     */
+    final public function cleanFieldsVal(){
+        $this->_easyORM->fields->cleanAllFieldsVal();
         return $this;
     }
 
@@ -229,9 +283,9 @@ final class EasyORM {
      * 批量设置值
      *
      * @param $values
-     * @return $this|void
+     * @return EasyORM
      */
-    public function setValues($values) {
+    final public function setValues($values) {
         if(!is_array($values)) return $this;
         foreach ($values as $key => $value) {
             $this->_easyORM->fields->hasField($key) && $this->_easyORM->fields->$key = $value;
@@ -243,7 +297,7 @@ final class EasyORM {
      * @param array|string $keys
      * @return EasyORM
      */
-    public function setPrivateKey($keys = array())
+    final public function setPrivateKey($keys = array())
     {
         if(empty($keys)) {
             return $this;
@@ -263,7 +317,7 @@ final class EasyORM {
      *
      * @return mixed
      */
-    public function getPrivateKey() {
+    final public function getPrivateKey() {
         return $this->_easyORM->privateKeys;
     }
 
@@ -271,9 +325,9 @@ final class EasyORM {
      * 移除主键
      *
      * @param array|string $keys
-     * @return $this
+     * @return EasyORM
      */
-    public function removePrivateKey($keys = array()) {
+    final public function removePrivateKey($keys = array()) {
         if(!is_array(key)) {
             if(isset($this->_easyORM->privateKeys[$keys])) unset($this->_easyORM->privateKeys[$keys]);
             return $this;
@@ -298,9 +352,9 @@ final class EasyORM {
     /**
      * 清除主键
      *
-     * @return $this
+     * @return EasyORM
      */
-    public function cleanPrivateKey() {
+    final public function cleanPrivateKey() {
         $this->_easyORM->privateKeys = array();
         return $this;
     }
@@ -311,7 +365,7 @@ final class EasyORM {
      * @param array $values 值
      * @return EasyORM
      */
-    public function setExcluded($values = array()){
+    final public function setExcluded($values = array()){
         if(!is_array($values)) {
             return $this;
         }
@@ -324,17 +378,38 @@ final class EasyORM {
     /**
      * 清除排除选项
      *
-     * @return void
+     * @return EasyORM
      */
-    public function cleanExcluded() {
+    final public function cleanExcluded() {
         $this->_easyORM->exclude = array();
         return $this;
     }
 
     /**
-     * @return Qii\Driver\Response|void
+     * 设置limit限制
+     *
+     * @param int $start limit start
+     * @param int $size size
+     * @return EasyORM
      */
-    public function save() {
+    final public function setLimit($start, $size = 20) {
+        $this->_easyORM->limit = [$start, $size];
+        return $this;
+    }
+
+    /**
+     * 清除limit限制
+     * @return EasyORM
+     */
+    final public function cleanLimit() {
+        $this->_easyORM->limit = array();
+        return $this;
+    }
+
+    /**
+     * @return Qii\Driver\Response|mixed
+     */
+    final public function save() {
         $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForSave());
         $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
         $verify = $this->_easyORM->rules->verify();
@@ -342,7 +417,7 @@ final class EasyORM {
             return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
             , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo']), 'message' => $verify['msg']));
         }
-        if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() > 0) {
+        if (($this->_easyORM->privateKeys || $this->_easyORM->exclude || $this->_easyORM->where) && $this->exist()->count() > 0) {
             return Response::Exist('save', array('_result' => \Qii::i(1511, join(',', $this->getPrivateValue()))));
         }
         $map = $this->_easyORM->rules->getValues();
@@ -354,13 +429,14 @@ final class EasyORM {
     }
 
     /**
-     * like(['key'])->count() like(['key']->lists()
+     * like([['key' => 'value'], ...]->count() like([['key' => 'value'], ...]->lists()
      * @param $values
-     * @return Qii\Driver\Response
+     * @return EasyORM
      */
-    public function like($values = array()) {
+    final public function like($values = array()) {
+        //未指定like字段
         if(!is_array($values)) {
-            return Response::Fail('like', array('_result' => '未指定like字段'));
+            return $this;
         }
         $query = array();
         foreach ($values as $key => $value) {
@@ -369,7 +445,7 @@ final class EasyORM {
             }
             $query[$key] = $value;
         }
-        $this->_easyORM->db->like($query);
+        $this->_easyORM->like = $query;
         return $this;
     }
 
@@ -378,17 +454,26 @@ final class EasyORM {
      *
      * @return Qii\Driver\Response
      */
-    public function count() {
+    final public function count() {
         $query = $this->getValueAsArray();
         if($this->_easyORM->where && is_array($this->_easyORM->where)) {
             foreach ($this->_easyORM->where as $key => $value) {
                 $query[$key] = $value;
             }
         }
+        //字段值作为where条件, 仅list和count用
+        $value = $this->getValueAsArray();
+        foreach ($value as $key => $val) {
+            $query[$key] = $val;
+        }
         $result = $this->_easyORM->db->fields(' count(1) as count')->limit(1)
             ->where($query)
+            ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
             ->selectOne($this->_easyORM->rules->getTable());
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
         return Response::Success('exist', array('_result' => $result));
     }
     /**
@@ -396,7 +481,7 @@ final class EasyORM {
      *
      * @return Qii\Driver\Response
      */
-    public function update() {
+    final public function update() {
         $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForUpdate());
         $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
         $verify = $this->_easyORM->rules->verify();
@@ -407,8 +492,13 @@ final class EasyORM {
         if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
             return  Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
         }
+        $where = $this->getWhereCondition();
+        //merge like 字段
+        if($this->_easyORM->like) {
+            $where = array_merge($this->_easyORM->like, $where);
+        }
         $map = $this->_easyORM->rules->getValues();
-        $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $this->getPrivateValue());
+        $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $where);
         if ($this->_easyORM->db->isError()) {
             return $this->_easyORM->db->getResponse();
         }
@@ -416,43 +506,128 @@ final class EasyORM {
     }
 
     /**
-     * 设置limit限制
+     * 指定字段的值 + step
      *
-     * @param int $start limit start
-     * @param int $size size
-     * @return $this
+     * $attr = new EasyDrive();
+     * $attr->visit = 1;
+     * $attr->where(['uid' => 1])->incr();
+     *
+     * @return mixed|Qii\Driver\Response
      */
-    public function setLimit($start, $size = 20) {
-        $this->_easyORM->limit = [$start, $size];
-        return $this;
+    final public function incr() {
+        $value = $this->_easyORM->fields->getValueAsArray();
+        if(!is_array($value) || count($value) == 0) {
+            return Response::FailUpdate('_incr', array('_result' => '未指定incr的key'));
+        }
+        $incrFields = array();
+        $privateKeys = $this->getPrivateKey();
+        foreach ($value as $key => $step) {
+            if($step <= 0) continue;
+            //去掉private的值
+            if(in_array($key, $privateKeys)) continue;
+            $incrFields[$key .':plus'] = $step;
+        }
+        if(count($incrFields) == 0){
+            return Response::FailUpdate('_incr', array('_result' => '未指定incr的步长'));
+        }
+
+        $fieldsAndValues = $this->getWhereCondition();
+
+        $result = $this->_easyORM->db
+            ->set($incrFields)
+            ->where($fieldsAndValues)
+            ->like($this->_easyORM->like)
+            ->exclude($this->_easyORM->exclude)
+            ->update($this->_easyORM->rules->getTable());
+
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
+        return Response::Success('_incr', array('_result' => $result));
     }
 
     /**
-     * 清除limit限制
-     * @return $this
+     * 更新数据只验证传过来的参数是否正确
+     *
+     * @return mixed|Qii\Driver\Response
      */
-    public function cleanLimit() {
-        $this->_easyORM->limit = array();
-        return $this;
+    final public function updateFields() {
+        $values = $this->_easyORM->fields->getValueAsArray();
+        $this->_easyORM->rules->addForceValidKey(array_keys($values));
+        $this->_easyORM->rules->addValues($values);
+        $verify = $this->_easyORM->rules->verify();
+        if(!$verify['valid']) {
+            return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
+            , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo']), 'message' => $verify['msg']));
+        }
+        if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
+            return Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
+        }
+        $where = $this->getWhereCondition();
+        //merge like 字段
+        if($this->_easyORM->like) {
+            $where = array_merge($this->_easyORM->like, $where);
+        }
+        $map = $this->_easyORM->rules->getValues();
+        $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $where);
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
+        return Response::Success('_update', array('_result' => $result));
     }
     /**
      * 返回所有数据
      *
      * @return mixed
      */
-    public function lists() {
+    final public function lists() {
         if($this->_easyORM->limit && count($this->_easyORM->limit) > 0) {
             $this->_easyORM->db->limit($this->_easyORM->limit[0], $this->_easyORM->limit[1]);
         }
-        $fieldsAndValues = $this->getValueAsArray();
-        if($this->_easyORM->where && is_array($this->_easyORM->where)) {
-            $fieldsAndValues = array_merge($fieldsAndValues, $this->_easyORM->where);
+        $fieldsAndValues = $this->getWhereCondition();
+        //字段值作为where条件, 仅list和count用
+        $value = $this->getValueAsArray();
+        foreach ($value as $key => $val) {
+            $fieldsAndValues[$key] = $val;
         }
-        return $this->_easyORM->db
+        $result = $this->_easyORM->db
             ->fields($this->_easyORM->queryFields)
             ->where($fieldsAndValues)
+            ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
+            ->orderBy($this->_easyORM->orderBy)
             ->selectRows($this->getTable());
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
+        return Response::Success('lists', array('_result' => $result));
+    }
+
+    /**
+     * 设置order by
+     * @param $orderBy
+     * @return EasyORM
+     */
+    final public function setOrderBy($orderBy) {
+        if(!$orderBy) {
+            return $this;
+        }
+        if(!is_array($orderBy)) {
+            $this->_easyORM->orderBy[] = $orderBy;
+        }else{
+            $this->_easyORM->orderBy = array_merge($this->_easyORM->orderBy, $orderBy);
+        }
+        return $this;
+    }
+
+    /**
+     * 清除order by 字段
+     *
+     * @return EasyORM
+     */
+    public function cleanOrderBy() {
+        $this->_easyORM->orderBy = [];
+        return $this;
     }
     /**
      * 删除指定数据
@@ -460,37 +635,41 @@ final class EasyORM {
      * @return Qii\Driver\Response
      */
     final public function remove() {
-        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude) {
+        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude && !$this->_easyORM->where) {
             return Response::FAIL('privateKey', \Qii::i(1513));
         }
-        $fieldsAndValues = [];
-        foreach ($this->_easyORM->privateKeys as $key) {
-            $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
+        $fieldsAndValues = $this->getWhereCondition();
+
+        //检查数据是否存在
+        if($this->exist()->count() == 0) {
+            return  Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
         }
+
         $result = $this->_easyORM->db
             ->where($fieldsAndValues)
+            ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
             ->remove($this->_easyORM->rules->getTable());
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
         return  Response::Success('remove', array('_result' => $result));
     }
 
     /**
      * 检查数据是否已经存在,并返回一行,只能根据主键查询
+     * privateKey 与 exclude、where 条件互斥
      *
      * @return \Qii\Driver\Response
      */
     final public function exist() {
-        if(!$this->_easyORM->privateKeys) {
+        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude && !$this->_easyORM->where) {
             $this->setPrivateKey(array_keys($this->getValueAsArray()));
         }
-        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude) {
+        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude && !$this->_easyORM->where) {
             return Response::FAIL('privateKey', \Qii::i(1513));
         }
-        $fieldsAndValues = [];
-        foreach ($this->_easyORM->privateKeys as $key) {
-            $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
-        }
-
+        $fieldsAndValues = $this->getWhereCondition();
         $verify = $this->_easyORM->rules->verifyFields($fieldsAndValues);
         if(!$verify['valid']) {
             return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
@@ -505,11 +684,31 @@ final class EasyORM {
         $result = $this->_easyORM->db->limit(1)
             ->fields($this->_easyORM->queryFields)
             ->where($fieldsAndValues)
+            ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
+            ->orderBy($this->_easyORM->orderBy)
             ->selectRow($this->_easyORM->rules->getTable());
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
         return Response::Success('exist', array('_result' => $result));
     }
 
+    /**
+     * 组合where查询条件
+     *
+     * @return array
+     */
+    final public function getWhereCondition() {
+        $fieldsAndValues = $this->getPrivateValue();
+        //merge where 条件的,这里不用merge,因为merge可能会merge出一个0索引的值
+        if($this->_easyORM->where && is_array($this->_easyORM->where)) {
+            foreach ($this->_easyORM->where as $key => $value) {
+                $fieldsAndValues[$key] = $value;
+            }
+        }
+        return $fieldsAndValues;
+    }
     /**
      * 使用此方法用于查询某一条数据的某一个字段
      * @useage getXxxByXxx _getXxxByxxx

+ 10 - 0
src/Driver/Fields.php

@@ -127,6 +127,16 @@ final class Fields
 		return $this->fields;
 	}
 
+    /**
+     * 清除所有的值
+     *
+     * @return void
+     */
+    public function cleanAllFieldsVal(){
+        foreach ($this->fields as $key => $val) {
+            unset($this->fields->$key);
+        }
+    }
 	/**
 	 * 以array的形式返回字段及值
 	 *

+ 20 - 20
src/Driver/Response.php

@@ -99,11 +99,11 @@ class Response
 	const FAIL_FOR_UPDATE = 104;
 	//删除失败
 	const FAIL_FOR_REMOVE = 105;
-	//类定义
+	//类定义
 	const UNDEFINED_CLASS = 106;
-	//方法定义
+	//方法定义
 	const UNDEFINED_METHOD = 107;
-	//是否有错误
+	//是否有错误,非DO_SUCCESS的都定义为有错,如果有其他优先级,请先验证其他结果
 	public $isError = false;
 	//状态码,对应上边的常量
 	public $code;
@@ -152,7 +152,7 @@ class Response
 	 * 失败
 	 * @param string $operate 操作类型
 	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function Fail($operate, $result)
 	{
@@ -162,8 +162,8 @@ class Response
 	/**
 	 * 记录已存在
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function Exist($operate, $result)
 	{
@@ -173,8 +173,8 @@ class Response
 	/**
 	 * 记录不存在
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function NotExist($operate, $result)
 	{
@@ -184,8 +184,8 @@ class Response
 	/**
 	 * 验证失败
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function FailValidate($operate, $result)
 	{
@@ -211,8 +211,8 @@ class Response
 	/**
 	 * 保存失败
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function FailSave($operate, $result)
 	{
@@ -231,8 +231,8 @@ class Response
 	/**
 	 * 更新失败
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function FailUpdate($operate, $result)
 	{
@@ -250,8 +250,8 @@ class Response
 	/**
 	 * 删除失败
 	 * @param string $operate 操作类型
-	 * @param mix $result 结果
-	 * @return Qii\Driver\Response
+	 * @param mixed $result 结果
+	 * @return Qii\Driver\Response|mixed
 	 */
 	public static function FailRemove($operate, $result)
 	{
@@ -359,7 +359,7 @@ class Response
 
 	/**
 	 * 返回错误信息,如果无错误即返回false
-	 * @return array
+	 * @return array|mixed
 	 */
 	public function getErrors()
 	{
@@ -374,7 +374,7 @@ class Response
 	public function isError()
 	{
 		$code = $this->code;
-		return $code == self::DO_SUCCESS ? false : true;
+		return !($code == self::DO_SUCCESS);
 	}
 
 	/**
@@ -383,8 +383,8 @@ class Response
 	public function __call($method, $args)
 	{
 		if (substr($method, 0, 3) == 'get') {
-			$propertty = strtolower(substr($method, 3));
-			if (property_exists($this, $propertty)) return $this->$propertty;
+			$property = strtolower(substr($method, 3));
+			if (property_exists($this, $property)) return $this->$property;
 		}
 		throw new MethodNotFound(\Qii::i(1101, $method), __LINE__);
 	}

+ 128 - 0
src/Library/Chinese2digits.php

@@ -0,0 +1,128 @@
+<?php
+namespace Qii\Library;
+
+/**
+ * 中文数字和阿拉伯数字相互转换
+ * Chinese2digits::SectionToChinese(1111);
+ * Chinese2digits::NumberToChinese(1111)
+ * Chinese2digits::ChineseToNumber("一千一百一十一")
+ */
+class Chinese2digits {
+    /**
+     * 最多4位数
+     *
+     * @param int $section
+     * @return string
+     */
+    public static function SectionToChinese($section) {
+        $chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
+        $chnUnitChar = ["", "十", "百", "千"];
+        $chnStr = "";
+        $unitPos = 0;
+        $zero = true;
+        while(true) {
+            if ($section <= 0) {
+                break;
+            }
+            $v = intval($section) % 10;
+            if ($v == 0) {
+                if (!$zero) {
+                    $zero = true;
+                    $chnStr = $chnNumChar[v] . $chnStr;
+                }
+            } else {
+                $zero = false;
+                $strIns = $chnNumChar[$v];
+                $strIns .= $chnUnitChar[$unitPos];
+                $chnStr = $strIns . $chnStr;
+            }
+            $unitPos++;
+            $section = intval($section / 10);
+        }
+        return $chnStr;
+    }
+
+    /**
+     * 数字转中文
+     *
+     * @param int $num 数字
+     * @return string
+     */
+    public static function NumberToChinese($num) {
+        $chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
+        $chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
+
+        $unitPos = 0;
+        $chnStr = "";
+        $needZero = false;
+
+        if ($num == 0) {
+            return $chnNumChar[0];
+        }
+        while (true) {
+            if ($num <= 0) {
+                break;
+            }
+            $section = $num % 10000;
+            if ($needZero) {
+                $chnStr = $chnNumChar[0] . $chnStr;
+            }
+            $strIns = self::SectionToChinese($section);
+            if ($section != 0) {
+                $strIns .= $chnUnitSection[$unitPos];
+            } else {
+                $strIns .= $chnUnitSection[0];
+            }
+
+            $chnStr = $strIns . $chnStr;
+            $needZero = ($section < 1000) && ($section > 0);
+            $num = intval($num / 10000);
+            $unitPos++;
+        }
+        return $chnStr;
+    }
+
+    /**
+     * 中文字符串数字
+     *
+     * @param string $chnStr 中文字符
+     * @return float|int
+     */
+    public static function ChineseToNumber($chnStr) {
+        if(preg_match("/^十/", $chnStr)) {
+            $chnStr = "一". $chnStr;
+        }
+        $chnNumCharDict = ["零" => 0, "一" => 1, "二" => 2, "三" => 3, "四" => 4, "五" => 5, "六" => 6, "七" => 7, "八" => 8, "九" => 9];
+        $chnNameValue = ["十" => ["value" => 10, "secUnit" => false]
+            , "百" => ["value" => 100, "secUnit" => false]
+            , "千" => ["value" => 1000, "secUnit" => false]
+            , "万" => ["value" => 10000, "secUnit" => true]
+            , "亿" => ["value" => 100000000, "secUnit" => true]
+        ];
+        $rtn = 0;
+        $section = 0;
+        $number = 0;
+        $str = preg_split("/(?<!^)(?!$)/u", $chnStr);
+        for ($i = 0; $i < count($str); $i++) {
+            $num = $chnNumCharDict[$str[$i]];
+            if ($num != 0) {
+                $number = $num;
+                if ($i == count($str)-1) {
+                    $section += $number;
+                }
+            } else {
+                $units = $chnNameValue[$str[$i]]['value'];
+                $secUnit = $chnNameValue[$str[$i]]['secUnit'];
+                if ($secUnit) {
+                    $section = ($section + $number) * $units;
+                    $rtn += $section;
+                    $section = 0;
+                } else {
+                    $section += ($number * $units);
+                }
+                $number = 0;
+            }
+        }
+        return $rtn + $section;
+    }
+}

+ 14 - 1
src/Library/Min.php

@@ -11,13 +11,26 @@ class Min
 
     const CSS = 'css';
     const JS = 'js';
+    protected $expired = 0;
 
     /**
      * Min constructor.
      */
     public function __construct()
     {
+        //默认5分钟
+        $this->expired = strtotime("now +5 minutes");
+    }
 
+    /**
+     * 设置过期时间 以当前时间戳+过期的秒数
+     *
+     * @param $minutes
+     * @return void
+     */
+    public function setExpired($minutes) {
+        $this->expired = strtotime("now +".$minutes." minutes");
+        return $this;
     }
 
     /**
@@ -114,7 +127,7 @@ class Min
             return;
         }
         header("Access-Control-Allow-Origin:'*'");
-        header("Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years")) ." GMT");
+        header("Expires: " . date("D, j M Y H:i:s", $this->expired) ." GMT");
         header($header[$type]);
         return $this;
     }

+ 1 - 0
src/Library/Upload.php

@@ -135,6 +135,7 @@ class Upload
         if (!isset($configure['prefix'])) $configure['prefix'] = '';
         
         $tmpFile = sys_get_temp_dir() . DS . time() . rand(10000, 1000000);
+        $name = rand(10000, 90000) . uniqid();
         if (isset($_SERVER['HTTP_CONTENT_DISPOSITION'])
             && preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',
                 $_SERVER['HTTP_CONTENT_DISPOSITION'], $info)