Forráskód Böngészése

Update : set 空值问题

朱金辉 4 hete
szülő
commit
2ce16f4c4a

+ 10 - 3
src/Driver/Base.php

@@ -386,16 +386,23 @@ class Base
         $operator = array("equal" => "%s`%s` = '%s'", "unequal" => "%s`%s` != '%s'", "plus" => "%s`%s` = %s`%s`+%s", "minus" => "%s`%s` = %s`%s`-%s", "multiply" => "%s`%s` = %s`%s`*%s", "divide" => "%s`%s` = %s`%s`/%s");
         foreach($set as $key => $val)
         {
-            $val = $this->setQuote($val);
+            if(strtoupper(gettype($val)) != 'NULL') {
+                $val = $this->setQuote($val);
+            }
             $alias = $this->getFieldAlias($key);
             $operate = $this->getFieldOperator($alias['name']);
             if($operate['operator'] == '') {
                 $operate['operator'] = $defaultOperator;
             }
+            $opt = $operator[$operate['operator']];
+            if(strtoupper(gettype($val)) == 'NULL') {
+                $val = 'NULL';
+                $opt = str_replace("'", '', $opt);
+            }
             if($operate['operator'] == 'equal') {
-                $this->sets[] = sprintf($operator[$operate['operator']], $alias['alias'], $operate['field'], $val);
+                $this->sets[] = sprintf($opt, $alias['alias'], $operate['field'], $val);
             }else{
-                $this->sets[] = sprintf($operator[$operate['operator']], $alias['alias'], $operate['field'], $alias['alias'], $operate['field'], $val);
+                $this->sets[] = sprintf($opt, $alias['alias'], $operate['field'], $alias['alias'], $operate['field'], $val);
             }
         }
 

+ 123 - 2
src/Driver/Entity/Base.php

@@ -18,6 +18,11 @@ class Base {
      * @var array $config
      */
     public static $config;
+    /**
+     * 主键
+     * @var mix $privateKeys null
+     */
+    public static $privateKeys = null;
     /**
      * 唯一字段
      *
@@ -34,6 +39,8 @@ class Base {
      * @var string 表别名
      */
     private $alias;
+
+    private $nullFields = [];
     /**
      * hooker for fields 查询字段/ where 查询条件 / order 排序 / cache 缓存 / with 关联查询
      * @var array $hooker
@@ -418,6 +425,15 @@ class Base {
         throw new \Exception('请设置唯一值');
     }
 
+    /**
+     * 设置主键
+     *
+     * @param mixed $privateKeys
+     * @return void
+     */
+    public function setPricateKey($privateKeys){
+        self::$privateKeys = $privateKeys;
+    }
     /**
      * 主键
      *
@@ -425,6 +441,9 @@ class Base {
      * @throws \Exception
      */
     public function primaryKey(){
+        if(self::$privateKeys !== null) {
+            return self::$privateKeys;
+        }
         throw new \Exception('请设置主键');
     }
 
@@ -499,6 +518,25 @@ class Base {
     public function validFieldsForUpdate() {
         throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
     }
+
+    /**
+     * 设置需要更新为空的字段列表
+     *
+     * @param $fields
+     * @return $this
+     */
+    public function setNull($fields) {
+        $tableFields = $this->fields();
+        foreach ($fields AS $field) {
+            if(!in_array($field, $tableFields)) {
+                continue;
+            }
+            $field = $this->entity()->convertToProperty($field);
+            $this->nullFields[] = $field;
+        }
+        return $this;
+    }
+
     /**
      * 获取不为空的属性
      *
@@ -519,6 +557,12 @@ class Base {
                 $fields[$field] = $this->$name;
             }
         }
+        if(count($this->nullFields) > 0) {
+            foreach ($this->nullFields AS $field) {
+                $field = $this->entity()->convertToField($field);
+                $fields[$field] = null;
+            }
+        }
         return $fields;
     }
 
@@ -616,7 +660,7 @@ class Base {
         $obj = new $class();
         $response = $this->info();
         if($response->isError()) {
-            $obj->Error = $response->getResult();
+            $obj->__Error = $response->getResult();
             return $obj;
         }
         $info = $response->getResult()['body'];
@@ -630,6 +674,42 @@ class Base {
         }
         return $obj;
     }
+
+    /**
+     * get 是否返回了错误
+     *
+     * @return bool
+     */
+    public function isError() {
+        if(isset($this->__Error)) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * get 返回的code
+     *
+     * @return int
+     */
+    public function getCode() {
+        if(!$this->isError()) {
+            return 0;
+        }
+        return isset($this->__Error['code']) ? $this->__Error['code'] : 0;
+    }
+
+    /**
+     * get 返回的msg
+     *
+     * @return string
+     */
+    public function getMessage() {
+        if(!$this->isError()) {
+            return '';
+        }
+        return isset($this->__Error['msg']) ? $this->__Error['msg'] : '';
+    }
     /**
      * 与get不同的这个返回的是Response
      *
@@ -870,7 +950,6 @@ class Base {
         //获取默认值
         //更新的时候不使用默认值
         //$values = array_merge($this->getDefaultValue(), $this->properties());
-
         $affectedRows = $this->db()->updateObject($this->prepareTable(), $this->properties(), $primaryKey);
         if($this->db()->isError()) {
             return Response::FailUpdate(static::class .'::'. __FUNCTION__,
@@ -1148,6 +1227,22 @@ class Base {
             }
         }
     }
+
+    /**
+     * 返回游标
+     *
+     * @param int | null $page 页码
+     * @param int | null $pageSize 页大小
+     * @return mixed
+     * @throws \Qii\Exceptions\InvalidParams
+     */
+    public function rs($page = null, $pageSize = null) {
+        $query = $this->createQuery()->orderBy($this->getOrderBy());
+        if($page && $pageSize) {
+            $query->limit($page, $pageSize);
+        }
+        return $query->rs($this->prepareTable());
+    }
     /**
      * listts
      * @param int $page 页码
@@ -1602,6 +1697,32 @@ class Base {
         return call_user_func_array(array($this->db(), $method), $args);
     }
 
+    /**
+     * 开始事务
+     *
+     * @return mixed
+     */
+    public function startTrans() {
+        return $this->db()->transaction();
+    }
+
+    /**
+     * 提交事务
+     *
+     * @return mixed
+     */
+    public function commit() {
+        return $this->db()->commit();
+    }
+
+    /**
+     * 回退
+     *
+     * @return mixed
+     */
+    public function rollback() {
+        return $this->db()->rollback();
+    }
     /**
      * 验证规则,由子类继承去修改
      *

+ 3 - 2
src/Driver/Entity/Entity.php

@@ -282,7 +282,7 @@ DOC;
      */
     public function uniqueKey(){
         if(self::\$uniqKeys !== null) return self::\$uniqKeys;
-        throw new \Exception('请设置唯一值');
+        throw new \Exception('请设置`{$tableInfo['table']}`表更新唯一值');
     }
 
     /**
@@ -292,12 +292,13 @@ DOC;
      * @throws \Exception
      */
     public function primaryKey(){
+        if(self::\$privateKeys !== null) return self::\$privateKeys;
 DOC;
         if($primaryKey) {
             $next[] = "\t\treturn array('". join("','", $primaryKey). "');";
         }else{
             $next[] = <<<DOC
-        throw new \Exception('请设置主键');
+        throw new \Exception('请设置`{$tableInfo['table']}`表主键');
 DOC;
         }
 

+ 5 - 4
src/Driver/Mysql/Driver.php

@@ -197,18 +197,19 @@ class Driver extends Base implements Intf
 
 	public function transaction()//事务处理
 	{
-		\mysql_query('begin');
+		return \mysql_query('begin');
 	}
 
 	public function commit()//事务提交
 	{
-		\mysql_query('commit');
-		\mysql_query('end');
+		$r1 = \mysql_query('commit');
+        $r2 = \mysql_query('end');
+        return $r1 && $r2;
 	}
 
 	public function rollback()//事务回滚
 	{
-		\mysql_query('rollback');
+		return \mysql_query('rollback');
 	}
 
 	public function affectedRows()//返回影响的行数

+ 6 - 4
src/Driver/Mysqli/Driver.php

@@ -205,7 +205,7 @@ class Driver extends Base implements Intf
 	 */
 	public function transaction()
 	{
-		\mysqli_query('begin');
+		return \mysqli_query('begin');
 	}
 
 	/**
@@ -213,8 +213,10 @@ class Driver extends Base implements Intf
 	 */
 	public function commit()
 	{
-		\mysqli_query('commit');
-		\mysqli_query('end');
+		$r1 = \mysqli_query('commit');
+		$r2 = \mysqli_query('end');
+
+        return $r1 && $r2;
 	}
 
 	/**
@@ -222,7 +224,7 @@ class Driver extends Base implements Intf
 	 */
 	public function rollback()
 	{
-		\mysqli_query('rollback');
+		return \mysqli_query('rollback');
 	}
 
 	/**

+ 1 - 1
src/Driver/Pdo/Driver.php

@@ -249,7 +249,7 @@ class Driver extends Base implements Intf
     public function commit()
     {
         $db = $this->connection->getWriteConnection();
-        $db->commit();
+        return $db->commit();
     }
 
     /**