Browse Source

update orm null value

朱金辉 3 weeks ago
parent
commit
6bce6ee3be
2 changed files with 57 additions and 7 deletions
  1. 33 3
      src/Driver/Base.php
  2. 24 4
      src/Driver/Entity/Base.php

+ 33 - 3
src/Driver/Base.php

@@ -188,6 +188,26 @@ class Base
     }
 
     /**
+     * 格式化插入值,新增null值插入
+     *
+     * @param $arr
+     * @return array|mixed
+     */
+    protected function formatInsertObject($arr) {
+        if(is_array($arr)) {
+            $values = array();
+            foreach ($arr as $v) {
+                if(strtoupper(gettype($val)) == 'NULL') {
+                    $values[] = 'null';
+                    continue;
+                }
+                $values[] = "'". $val . "'";
+            }
+            return $values;
+        }
+        return $arr;
+    }
+    /**
      * 插入数据
      * @param $table
      * @param $dataArray
@@ -204,7 +224,10 @@ class Base
         {
             throw new Variable(_i('Invalid %s format', 'data'), __LINE__);
         }
-        $this->executeSQL = $this->modelSQL = $sql = "INSERT INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES('" . join("', '", $replaceObj['values']) . "')";
+        // 针对 null 值单独处理,并将值的两端加上单引号 '
+        $values = $this->formatInsertObject($replaceObj['values']);
+        $this->executeSQL = $this->modelSQL = $sql = "INSERT INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES(". join(',', $values) . ")";
+
         $this->setQuery($sql);
         $this->setError();
         return $this->lastInsertId();
@@ -225,7 +248,10 @@ class Base
         {
             throw new Variable(_i('Invalid %s format', 'data'), __LINE__);
         }
-        $this->executeSQL = $this->modelSQL = $sql = "REPLACE INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES('" . join("', '", $replaceObj['values']) . "')";
+        // 针对 null 值单独处理,并将值的两端加上单引号 '
+        $values = $this->formatInsertObject($replaceObj['values']);
+
+        $this->executeSQL = $this->modelSQL = $sql = "REPLACE INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES(". join(',', $values) . ")";
         $rs = $this->setQuery($sql);
         $this->setError();
         return $this->AffectedRows($rs);
@@ -251,7 +277,11 @@ class Base
                 if (is_array($value)) {
                     throw new InvalidFormat(_i('Invalid %s format', $key), __LINE__);
                 }
-                $values[] = $this->setQuote($value);
+                if(strtoupper(gettype($value)) == 'NULL') {
+                    $values[] = null;
+                }else{
+                    $values[] = $this->setQuote($value);
+                }
             }
             return array('fields' => $keys, 'values' => $values);
         }

+ 24 - 4
src/Driver/Entity/Base.php

@@ -53,15 +53,28 @@ class Base {
      */
     private $defaultExpired = 600;
 
-    /** 当前执行的sql
+    /**
+     * 当前执行的sql
      *
      * @var string
      */
     private $calledSQL = "";
+    /**
+     * 最后插入数据的自增ID
+     *
+     * @var int $lastInsertID
+     */
+    private $lastInsertID;
 
     public function __construct(){
+        $this->cleanVars();
     }
 
+    final public function cleanVars() {
+        self::$uniqKeys = null;
+        self::$privateKeys = null;
+        self::$exclude = [];
+    }
     /**
      * 默认值,仅在新增数据的时候支持,更新不支持
      *
@@ -547,7 +560,7 @@ class Base {
      * @param mixed $privateKeys
      * @return void
      */
-    public function setPricateKey($privateKeys){
+    public function setPrivateKey($privateKeys){
         self::$privateKeys = $privateKeys;
     }
     /**
@@ -957,7 +970,7 @@ class Base {
         }
 
         $values = array_merge($this->getDefaultValue(), $this->properties());
-        $res = $this->db()->insertObject($this->prepareTable(), $values);
+        $this->lastInsertID = $res = $this->db()->insertObject($this->prepareTable(), $values);
 
         if($this->db()->isError()) {
             return Response::FailSave(static::class .'::'. __FUNCTION__,
@@ -974,6 +987,14 @@ class Base {
     }
 
     /**
+     * 获取最后一条插入记录的自增ID
+     *
+     * @return mixed
+     */
+    final public function lastInsertID() {
+        return $this->lastInsertID;
+    }
+    /**
      * 删除指定数据
      *
      * @return false|Response
@@ -1647,7 +1668,6 @@ class Base {
                 $useWhere  = true;
             }
         }
-
         foreach ($unique as $key) {
             $key = $this->entity()->convertToField($key);
             $property = $this->entity()->convertToProperty($key);