Quellcode durchsuchen

Update: 更新Model

朱金辉 vor 2 Jahren
Ursprung
Commit
97422e7323

+ 2 - 1
src/Base/Dispatcher.php

@@ -124,6 +124,7 @@ class Dispatcher
         $this->controllerCls->controllerId = $controller;
         $this->controllerCls->actionId = $action;
         $realAction = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);
+
         //查看是否设置了当前action的对应关系,如果设置了就走对应关系里边的,否则走当前类中的
         if ($this->controllerCls->actions
             && isset($this->controllerCls->actions[$action])
@@ -147,7 +148,7 @@ class Dispatcher
             if (!method_exists($this->actionCls, 'run')) {
                 throw new MethodNotFound(\Qii::i(1101, $this->controllerCls->actions[$action] . '->run'), __LINE__);
             }
-            return $this->actionCls->setResponse(array($this->actionCls, 'run'), array_slice($funcArgs, 1));
+            return $this->actionCls->setResponse(call_user_func_array(array($this->actionCls, 'run'), array_slice($funcArgs, 1)));
         }
         if(method_exists($this->controllerCls, 'initialization')) {
             call_user_func_array(array($this->controllerCls, 'initialization'), array($this->controllerCls));

+ 81 - 3
src/Base/Rules.php

@@ -1,6 +1,9 @@
 <?php
 namespace Qii\Base;
 
+use Qii\Exceptions\InvalidParams;
+use Qii\Exceptions\MethodNotFound;
+use Qii\Exceptions\TableException;
 use Qii\Execptions;
 
 /**
@@ -52,6 +55,8 @@ class Rules
      * 选择其中一个去验证
      */
     private $optionValidKey = array();
+
+    public $table = '';
     /**
      * 字段名称
      */
@@ -359,6 +364,46 @@ class Rules
         }
         return $data;
     }
+
+    /**
+     * 验证字段的值
+     *
+     * @param array $fields 需要验证的[字段=>值]
+     * @return array
+     */
+    public function verifyFields($fields) {
+        $data = array();
+        $data['data'] = $fields;
+        $data['code'] = 0;
+        $data['valid'] = true;
+
+        if(!is_array($fields)) {
+            $data['valid'] = true;
+            return $data;
+        }
+        $msg = [];
+        foreach ($fields as $key => $val) {
+            $rule = $this->get($key);
+            if(!$rule)
+            {
+                $rule['rules'] = array('required' => true);
+                $rule['message'] = array('required' => $key .'不能为空');
+            }
+            $result = $this->validate->verify(
+                array($key => $val),
+                array($key => $rule['rules']),
+                array($key => $rule['message'])
+            );
+            if($result !== true){
+                $data['valid'] = false;
+                $data['code'] = 20000;
+                $data['errorInfo'][$key] = $result;
+                $msg[] = $result;
+            }
+        }
+        if(count($msg) > 0) $data['msg'] = join("\n", $msg);
+        return $data;
+    }
     /**
      * 验证数据,验证将返回数据及验证结果
      * @return bool
@@ -393,6 +438,7 @@ class Rules
                 $data['msg'] = _i($data['code']);
                 return $data;
         }
+        $msg = array();
         foreach($this->forceValidKey AS $key)
         {
             $rule = $this->get($key);
@@ -410,11 +456,12 @@ class Rules
                 $data['valid'] = false;
                 $data['code'] = 20000;
                 $data['errorInfo'][$key] = $result;
+                $msg[] = $result;
             }
         }
-        if($data['code'] > 0)
+        if(count($msg) > 0)
         {
-            $data['msg'] = _i($data['code']);
+            $data['msg'] = join("\n", $msg);
         }
         return $data;
     }
@@ -484,7 +531,38 @@ class Rules
             $this->addRules($value[0], $validKey, $value[2], $value[1]);
         }
     }
-
+    /**
+     * 设置表名,主要针对分表的
+     */
+    public function setTable($table) {
+        if(!$table) throw new InvalidParams("table is null");
+        $this->table = $table;
+    }
+    /**
+     * 获取当前表名
+     *
+     * @return void
+     */
+    public function getTable(){
+        if (!$this->table) throw new TableException("table is null");
+        return $this->table;
+    }
+    /**
+     * 保存验证
+     *
+     * @return array
+     */
+    public function getValidFieldsForSave(){
+        return [];
+    }
+    /**
+     * 更新数据验证
+     *
+     * @return array
+     */
+    public function getValidFieldsForUpdate(){
+        return [];
+    }
     /**
      * 快捷添加验证
      * @param string $method 验证方法

+ 177 - 0
src/Driver/EasyDrive.php

@@ -0,0 +1,177 @@
+<?php
+/**
+ * 简易数据库操作类
+ *
+ * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-12-10 14:35
+ *
+ * usage:
+ *
+ * @method setPrivateKey(array $data);
+ */
+namespace Qii\Driver;
+
+use Qii\Exceptions\InvalidParams;
+
+/**
+ * 用法 :
+ * 首先需要创建 _ormRules 文件 rules/user.php
+    use Qii\Base\Rules;
+    class user extends Rules {
+        public $table = 'user';
+        pubic function fields(){
+           return ['uid', 'nickname', 'password'];
+        }
+        public function constants() {
+            $this->addRequiredFields(
+                ['id', '用户ID不能为空'],
+                ['nickname', '昵称不能为空'],
+                ['password', '密码不能为空'],
+            );
+
+            $this->addNumberFields(
+                ['uid', '用户ID必须是数字']
+            );
+            $this->addSafeFields(
+                ['nickname', '昵称格式不正确,不能包含怪字符']
+            );
+            $this->addPasswordFields(
+                ['password', '密码格式不正确']
+            );
+        }
+    }
+    use Qii\Driver\EasyDrive;
+
+    class user extends EasyDrive {
+
+        public $_ormRules = 'rules\user';
+        public function __construct()
+        {
+            parent::__construct();
+        }
+    }
+
+    $user = new user();
+    $user->uid = 1;
+    $user->nickname = 'admin';
+    $user->password = md5('asdfadsfasdadfadf');
+    $response = $user->save();
+
+    $user->setPrivatekey(['uid']);
+    $response = $user->exist();
+
+    if($response->isError()) {
+        return ['code' => $response->getCode(), 'msg' => $response->getMessage()]
+   }
+   if($response->count() > 0) {
+        return $response->getResult();
+   }
+ *
+ *
+ */
+class EasyDrive {
+    public $_ormRules = '';
+    protected $_easyORM;
+    protected $db;
+    /**
+     * @throws InvalidParams
+     */
+    public function __construct(){
+        if ($this->_ormRules == '') {
+            throw new InvalidParams('_ormRules is null');
+        }
+        $this->_easyORM = new \Qii\Driver\EasyORM(_loadClass($this->_ormRules));
+        $this->db = $this->getDB();
+    }
+
+    /**
+     * 获取DB属性
+     * @return mixed
+     */
+    public function getDB() {
+        return $this->_easyORM->getDB();
+    }
+
+    /**
+     * 获取规则
+     *
+     * @return mixed
+     */
+    public function getRules() {
+        return $this->_easyORM->getRules();
+    }
+
+    /**
+     * 获取所有的字段
+     *
+     * @return mixed
+     */
+    public function getFields() {
+        return $this->_easyORM->getFields();
+    }
+
+    /**
+     * 设置表名,将rules暴露到外边
+     *
+     * @param string $table 表名
+     * @return mixed
+     */
+    public function setTable($table) {
+        $this->_easyORM->setTable($table);
+        return $this;
+    }
+
+    /**
+     * 返回表名
+     *
+     * @return mixed
+     */
+    public function getTable() {
+        return $this->_easyORM->getTable();
+    }
+
+    /**
+     * 获取字段及值
+     *
+     * @return mixed
+     */
+    public function getValues() {
+        return $this->_easyORM->getValues();
+    }
+
+    public function __set($name, $value) {
+        $this->_easyORM->$name = $value;
+    }
+
+    /**
+     * 设置主键
+     *
+     * @param array $array 数组形式
+     * @return EasyORM
+     */
+    public function setPrivateKey($array) {
+        $this->_easyORM->setPrivateKey($array);
+        return $this;
+    }
+
+    /**
+     * 设置排除的内容
+     *
+     * @param array $array
+     * @return $this
+     */
+    public function setExclude($array) {
+        $this->_easyORM->setExcluded($array);
+        return $this;
+    }
+
+    /**
+     * __call 自动调用_easyORM中的方法
+     *
+     * @param string $method
+     * @param mixed $args
+     * @return mixed
+     */
+    public function __call($method, $args) {
+        return call_user_func_array(array($this->_easyORM, $method), $args);
+    }
+}

+ 422 - 0
src/Driver/EasyORM.php

@@ -0,0 +1,422 @@
+<?php
+namespace Qii\Driver;
+
+use Qii\Exceptions\InvalidParams;
+use stdClass;
+
+/**
+ * 使用方法
+ *
+ *
+ */
+final class EasyORM {
+    protected $_easyORM = array();
+
+    /**
+     * @throws InvalidParams
+     */
+    public function __construct(\Qii\Base\Rules $rules) {
+        $this->clean();
+        $this->_easyORM->db = \Qii::getInstance("\Qii\Driver\Model")->db;
+        $this->setRules($rules);
+        $this->setFields($rules->fields());
+        $this->setResponse(new Response());
+    }
+
+    /**
+     * 获取db属性
+     *
+     * @return mixed
+     */
+    public function getDB() {
+        return $this->_easyORM->db;
+    }
+
+    /**
+     * 设置验证规则
+     *
+     * @param \Qii\Base\Rules $rules
+     * @return void
+     */
+    public function setRules(\Qii\Base\Rules $rules) {
+        $this->_easyORM->rules = $rules;
+    }
+
+    /**
+     * 返回 rules
+     *
+     * @return mixed
+     */
+    public function getRules() {
+        return $this->_easyORM->rules;
+    }
+
+    /**
+     * 设置需要操作的表名称
+     *
+     * @param string $table 表名
+     * @return mixed
+     */
+    public function setTable($table) {
+        return $this->_easyORM->rules->setTable($table);
+    }
+
+    /**
+     * 获取表名
+     * 
+     * @return mixed
+     */
+    public function getTable() {
+        return $this->_easyORM->rules->getTable();
+    }
+    /**
+     * 设置表字段
+     *
+     * @param array $fields
+     * @return void
+     * @throws
+     */
+    public function setFields($fields = array()) {
+        if(!is_array($fields)) {
+            throw new InvalidParams("数据表字段不能为空");
+        }
+        $this->_easyORM->fields = new Fields($fields);
+    }
+
+    /**
+     * 获取所有字段
+     *
+     * @return mixed
+     */
+    public function getFields() {
+        return $this->_easyORM->fields->getFields();
+    }
+
+    /**
+     * 获取字段的值
+     * @param string $field 字段名称
+     * @return mixed
+     */
+    public function getFieldValue($field) {
+        return $this->_easyORM->fields->getField($field);
+    }
+
+    /**
+     * 获取所有字段和值
+     *
+     * @return mixed
+     */
+    public function getValues() {
+        return $this->_easyORM->fields->getValues();
+    }
+    /**
+     * @param Response $response
+     * @return void
+     */
+    public function setResponse(Response $response) {
+        $this->_easyORM->response = $response;
+    }
+
+    /**
+     * @return EasyORM
+     */
+    protected function clean(){
+        $this->_easyORM = new stdClass();
+        $this->_easyORM->rules = null;
+        $this->_easyORM->db = null;
+        $this->_easyORM->fields = null;
+        $this->_easyORM->response = null;
+        $this->_easyORM->privateKeys = array();
+        $this->_easyORM->exclude = array();
+        return $this;
+    }
+
+    /**
+     * 设置表字段
+     *
+     * @param $name
+     * @param $val
+     * @return $this
+     */
+    public function __set($name, $val)
+    {
+        $this->_easyORM->fields->hasField($name) && $this->_easyORM->fields->$name = $val;
+        return $this;
+    }
+
+    /**
+     * 批量设置值
+     *
+     * @param $values
+     * @return $this|void
+     */
+    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;
+        }
+        return $this;
+    }
+    /**
+     * 设置 private key,主键必须有设置值,否则不接收
+     * @param array|string $keys
+     * @return EasyORM
+     */
+    public function setPrivateKey($keys = array())
+    {
+        if(empty($keys)) {
+            return $this;
+        }
+        if(!is_array($keys)) {
+            $this->_easyORM->fields->hasField($keys) && $this->_easyORM->privateKeys[] = $keys;
+            return $this;
+        }
+        foreach ($keys as $key) {
+            $this->_easyORM->fields->hasField($key) && $this->_easyORM->privateKeys[] = $key;
+        }
+        return $this;
+    }
+
+    /**
+     * 移除主键
+     *
+     * @param array|string $keys
+     * @return $this
+     */
+    public function removePrivateKey($keys = array()) {
+        if(!is_array(key)) {
+            if(isset($this->_easyORM->privateKeys[$keys])) unset($this->_easyORM->privateKeys[$keys]);
+            return $this;
+        }
+        foreach ($keys as $key) {
+           if(isset($this->_easyORM->privateKeys[$key])) unset($this->_easyORM->privateKeys[$key]);
+        }
+        return $this;
+    }
+    /**
+     * 获取主键对应的值
+     * @return array
+     */
+    final protected function getPrivateValue()
+    {
+        $data = array();
+        foreach ($this->_easyORM->privateKeys AS $key) {
+            $data[$key] = $this->_easyORM->fields->getField($key);
+        }
+        return $data;
+    }
+
+    /**
+     * 设置排除的数据列
+     *
+     * @param array $values 值
+     * @return EasyORM
+     */
+    public function setExcluded($values = array()){
+        if(!is_array($values)) {
+            return $this;
+        }
+        foreach ($values as $key => $value) {
+            $this->_easyORM->fields->hasField($key) && $this->_easyORM->exclude[$key] = $value;
+        }
+        return $this;
+    }
+
+    /**
+     * @return Qii\Driver\Response|void
+     */
+    public function save() {
+        $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForSave());
+        $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
+        $verify = $this->_easyORM->rules->verify();
+        if(!$verify['valid']) {
+            return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], '验证失败')
+            , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
+        }
+        if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() > 0) {
+            return Response::Exist('save', array('_result' => \Qii::i(1511, join(',', $this->getPrivateValue()))));
+        }
+        $map = $this->_easyORM->rules->getValues();
+        $result = $this->_easyORM->db->insertObject($this->_easyORM->rules->getTable(), $map);
+        if($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
+        return Response::Success('save', array('_result' => $result));
+    }
+
+    /**
+     * 更新数据
+     *
+     * @return Qii\Driver\Response
+     */
+    public function update() {
+        $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForUpdate());
+        $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
+        $verify = $this->_easyORM->rules->verify();
+        if(!$verify['valid']) {
+            return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], '验证失败')
+            , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
+        }
+        if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
+            return  Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
+        }
+        $map = $this->_easyORM->rules->getValues();
+        $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $this->getPrivateValue());
+        if ($this->_easyORM->db->isError()) {
+            return $this->_easyORM->db->getResponse();
+        }
+        return Response::Success('_update', array('_result' => $result));
+    }
+
+    /**
+     * 删除指定数据
+     *
+     * @return Qii\Driver\Response
+     */
+    final public function remove() {
+        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude) {
+            return Response::FAIL('privateKey', \Qii::i(1513));
+        }
+        $fieldsAndValues = [];
+        foreach ($this->_easyORM->privateKeys as $key) {
+            $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
+        }
+        $result = $this->_easyORM->db
+            ->where($fieldsAndValues)
+            ->exclude($this->_easyORM->exclude)
+            ->remove($this->_easyORM->rules->getTable());
+        return  Response::Success('remove', array('_result' => $result));
+    }
+
+    /**
+     * 检查数据是否已经存在,并返回一行,只能根据主键查询
+     *
+     * @return \Qii\Driver\Response
+     */
+    final public function exist() {
+        if(!$this->_easyORM->privateKeys &&  !$this->_easyORM->exclude) {
+            return Response::FAIL('privateKey', \Qii::i(1513));
+        }
+        $fieldsAndValues = [];
+        foreach ($this->_easyORM->privateKeys as $key) {
+            $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
+        }
+
+        $this->_easyORM->rules->addValues($fieldsAndValues);
+        $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'])));
+        }
+
+        if($this->_easyORM->exclude) {
+            $verify = $this->_easyORM->rules->verifyFields($this->_easyORM->exclude);
+            if (!$verify['valid'])  return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'],  $verify['msg'])
+            , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
+        }
+        $result = $this->_easyORM->db->limit(1)
+            ->where($fieldsAndValues)
+            ->exclude($this->_easyORM->exclude)
+            ->selectRow($this->_easyORM->rules->getTable());
+        return  Response::Success('exist', array('_result' => $result));
+    }
+
+    /**
+     * 使用此方法用于查询某一条数据的某一个字段
+     * @useage getXxxByXxx _getXxxByxxx
+     *     _getRowById:返回所有字段; _getRowsById:通过id获取所有匹配的数据
+     *     _getRowsByFields(['user_id', 'email']) 返回所有匹配的行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
+     *     _getRowByFields(['user_id', 'email']) 返回匹配的一行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
+     *     _getRowByUserId(1) 通过user_id查询,返回所有字段
+     *     getNameById:通过id获取name的值; getEmailById:通过id获取email;
+     *     getEmailAddressByUserId 通过user_id查询email_address
+     *     getEmailByUserId(1) 通过user_id查询,返回email字段
+     * 备注:以_开头的才会去走getRow, getRows方法去取所有字段,目前仅支持All, Row, Rows这几个方法
+     * @param  string $method [description]
+     * @param  mixed $args 请求的参数
+     * @return Response
+     */
+    final public function __call($method, $args)
+    {
+        $selectType = 'normal';
+        if (substr($method, 0, 1) == '_') {
+            $selectType = 'system';
+            $method = substr($method, 1);
+        }
+        preg_match('/^(get)(.*)(By)(.*)/', $method, $matches);
+
+        if ($matches && count($matches) == 5 && $matches[1] == 'get') {
+            //大写字母匹配下划线,字段中统一用小写字母,在查询的时候使用驼峰结构
+            //如:getEmailAddressByUserId 通过user_id查询email_address
+            $field = strtolower(preg_replace('/(?<!^)([A-Z])/', '_$1', $matches[2]));
+
+            $method = 'selectRow';
+            if ($field == 'rows' && $selectType == 'system') $method = 'selectRows';
+            if ($field == 'row' && $selectType == 'system') $method = 'selectRow';
+            if (in_array($field, array('all', 'row', 'rows')) && $selectType == 'system') {
+                $field = $this->getFields();
+            }
+            $value = $this->_easyORM->db->setQuote(array_shift($args));
+            $name = strtolower(strtolower(preg_replace('/(?<!^)([A-Z])/', '_$1', $matches[4])));
+            $whereArray = array();
+            if ($selectType == 'system' && $name == 'fields') {
+                //优先使用参数,参数没有就使用设置的fields
+                // _getRowsByFields() 返回所有匹配的行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
+                // _getRowByFields() 返回匹配的一行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
+                if(!$value) {
+                    $value = $this->_easyORM->fields->getValues();
+                }
+                foreach ($value AS $val) {
+                    $whereArray[$val] = $this->_easyORM->fields->hasField($val) ? $this->_easyORM->fields->getField($val) : '';
+                }
+            } else {
+                $whereArray[$name] = $value;
+            }
+            $where = array();
+            foreach ($whereArray AS $key => $val) {
+                if($this->_easyORM->fields->hasField($key)) $where[$key] = $val;
+            }
+
+            $result = $this->_easyORM->db->fields($field)->where($where)->$method($this->_easyORM->rules->getTable());
+            if ($this->_easyORM->db->isError()) {
+                return $this->_easyORM->db->getResponse();
+            }
+            return Response::Success($method, array('_result' => $result));
+        }
+
+        preg_match('/^(exec)(.*)/', $method, $matches);
+
+        if ($matches && count($matches) == 3) {
+            $alias = lcfirst($matches[2]);
+            if (method_exists($this->_easyORM->db, $alias)) {
+                $result = $this->_easyORM->db->{$matches[2]}($this->_easyORM->rules->getTable(), $this->getFields());
+                if ($this->_easyORM->db->isError()) {
+                    return $this->_easyORM->db->getResponse();
+                }
+                return Response::Success($matches[2], array('_result' => $result));
+            }
+            if ($this->_easyORM->db->getAlias($method) && method_exists($this->_easyORM->db, $this->_easyORM->db->getAlias($method))) {
+                $this->_easyORM->db->whereArray($this->getFields());
+                $result = $this->_easyORM->db->{$this->_easyORM->db->getAlias($method)}($this->_easyORM->rules->getTable());
+                if ($this->_easyORM->db->isError()) {
+                    return $this->_easyORM->db->getResponse();
+                }
+                return Response::Success($this->_easyORM->db->getAlias($method), array('_result' => $result));
+            }
+        }
+        //访问方法的别名
+        if ($this->_easyORM->db->getAlias($method)) {
+            if (method_exists($this->_easyORM->db, $this->_easyORM->db->getAlias($method))) {
+                $result = call_user_func_array(array($this->_easyORM->db, $this->_easyORM->db->getAlias($method)), $args);
+                if ($this->_easyORM->db->isError()) {
+                    return $this->_easyORM->db->getResponse();
+                }
+                return Response::Success($this->_easyORM->db->getAlias($method), array('_result' => $result));
+            }
+            \Qii::setError(false, __LINE__, 1106, 'Model', 'Alias ' . $this->_easyORM->db->getAlias($method) . ' does not exist.', print_r($args, true));
+            return Response::UndefinedMethod('__call', $this->_easyORM->db->getAlias($method));
+        }
+        \Qii::setError(false, __LINE__, 1106, 'Model', $method, print_r($args, true));
+        return Response::UndefinedMethod('__call', $method);
+    }
+}

+ 39 - 9
src/Driver/Fields.php

@@ -25,8 +25,8 @@ final class Fields
 	public function __construct($fields)
 	{
 		if (!is_array($fields) && count($fields) == 0) throw new \Exception(\Qii::i(1508), __LINE__);
-		$this->keys = new \stdClass();
-		$this->fields = $fields;
+		$this->keys = $fields;
+		$this->fields = new \stdClass();
 		return $this;
 	}
 
@@ -39,20 +39,40 @@ final class Fields
 	 */
 	public function __set($name, $val)
 	{
-		if (in_array($name, $this->fields)) $this->keys->$name = $val;
+		if (in_array($name, $this->keys)) $this->fields->$name = $val;
 		return $this;
 	}
 
+    /**
+     * unset 关键字
+     * @param string $key 需要移除的关键字
+     * @return void
+     */
+    public function __unset($key){
+        if(isset($this->fields->$key)) unset($this->fields->$key);
+    }
+    /**
+     * 批量设置字段的值
+     * @param array $data
+     * @return $this
+     */
+    public function setFieldsVal(array $data)
+    {
+        foreach ($data AS $name => $val) {
+            $this->fields->$name = $val;
+        }
+        return $this;
+    }
+
 	/**
 	 * 判断是否存在相关键值
 	 *
 	 * @param $field
 	 * @return bool
 	 */
-	public function isField($field)
+	public function hasField($field)
 	{
-		if (isset($this->keys->$field)) return true;
-		return false;
+		return in_array($field, $this->keys);
 	}
 
 	/**
@@ -63,10 +83,20 @@ final class Fields
 	 */
 	public function getField($field)
 	{
-		if (isset($this->keys->$field)) return $this->keys->$field;
+        if (!$field) return null;
+		if (isset($this->fields->$field)) return $this->fields->$field;
 		return null;
 	}
 
+    /**
+     * 获取所有字段
+     *
+     * @return mixed
+     */
+    public function getFields() {
+        return $this->keys;
+    }
+
 	/**
 	 * 获取字段及值
 	 *
@@ -74,7 +104,7 @@ final class Fields
 	 */
 	public function getValues()
 	{
-		return $this->keys;
+		return $this->fields;
 	}
 
 	/**
@@ -84,7 +114,7 @@ final class Fields
 	 */
 	public function getValueAsArray()
 	{
-		return (array)$this->keys;
+		return (array)$this->fields;
 	}
 
 	public function __call($method, $argvs)

+ 4 - 2
src/Driver/Model.php

@@ -3,6 +3,8 @@ namespace Qii\Driver;
 
 use Qii\Autoloader\Import;
 use Qii\Autoloader\Psr4;
+use Qii\Config\Consts;
+use Qii\Config\Register;
 
 /**
  * 数据库分发器
@@ -77,7 +79,7 @@ class Model
         $this->_language = Psr4::getInstance()->loadClass('\Qii\Language\Loader');
         $this->_request = Psr4::getInstance()->loadClass('Qii\Request\Http');
         $this->_helper = Psr4::getInstance()->loadClass('Qii\Autoloader\Helper');
-        $this->_dbInfo = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_DB));
+        $this->_dbInfo = Register::getAppConfigure(Register::get(Consts::APP_DB));
         if (isset($this->_dbInfo['driver'])) {
             $this->_driver = $this->_dbInfo['driver'];
         }
@@ -135,7 +137,7 @@ class Model
      * 设置规则
      * @param array $rules
      */
-    public function setRules(\Qii\Driver\Rules $rules)
+    public function setRules(Rules $rules)
     {
         if (empty($rules)) throw new \Exception(\Qii::i('Please set rules first'), __LINE__);
         $this->rules = $rules;

+ 54 - 1
src/Driver/Response.php

@@ -192,6 +192,22 @@ class Response
 		return self::Instance(self::FAIL_FOR_VALIDATE, $operate, $result);
 	}
 
+    /**
+     * 返回当前 Response 状态码
+     *
+     * @return int
+     */
+    public function getCode() {
+        return $this->code;
+    }
+    /**
+     * 是否是验证失败
+     * @return bool
+     */
+    public function isFailValidate() {
+        return $this->getCode() == self::FAIL_FOR_VALIDATE;
+    }
+
 	/**
 	 * 保存失败
 	 * @param string $operate 操作类型
@@ -203,6 +219,15 @@ class Response
 		return self::Instance(self::FAIL_FOR_SAVE, $operate, $result);
 	}
 
+    /**
+     * 是否保存失败
+     *
+     * @return bool
+     */
+    public function isFailSave() {
+        return $this->getCode() == self::FAIL_FOR_SAVE;
+    }
+
 	/**
 	 * 更新失败
 	 * @param string $operate 操作类型
@@ -214,6 +239,14 @@ class Response
 		return self::Instance(self::FAIL_FOR_UPDATE, $operate, $result);
 	}
 
+    /**
+     * 是否更新失败
+     *
+     * @return bool
+     */
+    public function isFailUpdate() {
+        return $this->getCode() == self::FAIL_FOR_UPDATE;
+    }
 	/**
 	 * 删除失败
 	 * @param string $operate 操作类型
@@ -225,6 +258,14 @@ class Response
 		return self::Instance(self::FAIL_FOR_REMOVE, $operate, $result);
 	}
 
+    /**
+     * 是否删除失败
+     *
+     * @return bool
+     */
+    public function isFailRemove() {
+        return $this->getCode() == self::FAIL_FOR_REMOVE;
+    }
 	/**
 	 * 方法未定义
 	 * @param string $operate 操作类型
@@ -253,7 +294,7 @@ class Response
 	public static function Instance($code, $operate, $result)
 	{
 		$data = array('code' => $code, 'body' => array('operate' => $operate, 'result' => $result));
-		return (new \Qii\Driver\Response())->set($data);
+		return (new Response())->set($data);
 	}
 
 	/**
@@ -278,6 +319,18 @@ class Response
 		return $this->body['result'];
 	}
 
+    /**
+     * 获取返回的结果
+     *
+     * @return int
+     */
+    public function count() {
+        $value = $this->getResult();
+        if(!$value) return 0;
+        if(is_array($value)) return count($value);
+        return 1;
+    }
+
 	/**
 	 * 返回错误信息
 	 */

+ 1 - 1
src/Exceptions/MethodNotFound.php

@@ -1,7 +1,7 @@
 <?php
 namespace Qii\Exceptions;
 
-if (class_exists('\Qii\Exceptions\InvalidParams', false)) {
+if (class_exists('\Qii\Exceptions\MethodNotFound', false)) {
     return;
 }
 

+ 1 - 1
src/Library/Validate.php

@@ -473,7 +473,7 @@ class Validate
 	 */
 	public function setsArray($str, $sets)
 	{
-		return in_array($str, $sets);
+		return $str && in_array($str, $sets);
 	}
 
     /**