Browse Source

Update: 优化Entity类中的查询功能

zjh 4 tháng trước cách đây
mục cha
commit
68aee01d0e

+ 28 - 3
src/Driver/Base.php

@@ -1,5 +1,6 @@
 <?php
 namespace Qii\Driver;
+use controller\error;
 use Qii\Autoloader\Psr4;
 use Qii\Exceptions\InvalidFormat;
 use Qii\Exceptions\InvalidParams;
@@ -517,7 +518,7 @@ class Base
      */
     public function handleWhereFields($where) {
         if(!is_array($where)) {
-            return $this;
+            return $where;
         }
         $fields = [];
         foreach ($where as $key => $val) {
@@ -532,8 +533,29 @@ class Base
             }
             if($len > $maxLen) {
                 $arr1 = array_slice($arr, 0, count($arr) - $maxLen);
-                foreach ($arr1 as $key) {
-                    $fields[$key . $opt] = $val;
+                if(is_array($val)) {
+                    $arr2 = [];
+                    foreach ($val as $k1 => $v1) {
+                        $keyArr = explode(':', $k1);
+                        $lenKey = count($keyArr);
+                        if($lenKey > 2) {
+                            $opt1 = $keyArr[$lenKey-1];
+                            $keyArrSlice = array_slice($keyArr, 0, $lenKey-1);
+                            foreach ($keyArrSlice as $k2 => $v2) {
+                                $arr2[$v2 .':'. $opt1] = $v1;
+                                if($k2 < $lenKey-2) {
+                                    $arr2[] = 'or';
+                                }
+                            }
+                        }
+                    }
+                    if(count($arr2) > 0) {
+                        $fields[] = $arr2;
+                        continue;
+                    }
+                }
+                foreach ($arr1 as $key1) {
+                    $fields[$key1 . $opt] = $val;
                 }
             }
         }
@@ -1366,6 +1388,9 @@ class Base
                 }
                 if($this->isOperator($v[$j]))
                 {
+                    if(empty($operateCondition)) {
+                        continue;
+                    }
                     $operateCondition[] = $v[$j];
                     continue;
                 }

+ 29 - 5
src/Driver/Entity/Base.php

@@ -76,6 +76,13 @@ class Base {
      */
     private $tableName = null;
 
+    /**
+     * 错误信息
+     *
+     * @var array $__Error {code:0, msg:'', body:mixed}
+     */
+    public $__Error;
+
     public function __construct(){
         $this->cleanVars();
     }
@@ -1200,6 +1207,23 @@ class Base {
             ]
         );
     }
+
+    /**
+     * 排除操作符
+     *
+     * @param array $array1 数组1
+     * @param array $array2 数组2
+     * @return array
+     */
+    final public function array_diff_assoc($array1, $array2) {
+        $arr = array_diff_assoc($array1, $array2);
+        foreach($arr as $k => $v) {
+            if(is_numeric($k) && in_array($v, ['or', 'and'])) {
+                unset($arr[$k]);
+            }
+        }
+        return $arr;
+    }
     /**
      * 更新
      *
@@ -1214,8 +1238,8 @@ class Base {
         //检查是否有重复的
         list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
         // 检查 unique 是否已经存在相关数据
-        $diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
-        $diffOr = array_diff_assoc($uniqueOr, $primaryKey);
+        $diffWhere = $this->array_diff_assoc($uniqueWhere, $primaryKey);
+        $diffOr = $this->array_diff_assoc($uniqueOr, $primaryKey);
         /*print_r(
             [
                 'where' => $uniqueWhere,
@@ -1295,8 +1319,8 @@ class Base {
             ]
         );*/
         // 检查 unique 是否已经存在相关数据
-        $diffWhere = array_diff_assoc($uniqueWhere, $primaryKey);
-        $diffOr = array_diff_assoc($uniqueOr, $primaryKey);
+        $diffWhere = $this->array_diff_assoc($uniqueWhere, $primaryKey);
+        $diffOr = $this->array_diff_assoc($uniqueOr, $primaryKey);
         /*$diffExclude = array_diff_assoc($exclude, $primaryKey);
 
         print_r(
@@ -1365,7 +1389,7 @@ class Base {
             }
             $incr[$key] = $value;
         }
-        $diff = array_diff_assoc($incr, $primary);
+        $diff = $this->array_diff_assoc($incr, $primary);
         if(count($diff) == 0) {
             return Response::Fail(self::class .'::'. __FUNCTION__,
                 [

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

@@ -1,8 +1,5 @@
 <?php
 namespace Qii\Driver\Entity;
-
-use entity\SysDictType;
-
 /**
  * 通过数据表生成 entity
  * 使用方法:

+ 27 - 0
src/Driver/TraitDatabase.php

@@ -62,6 +62,33 @@ trait TraitDatabase
         return $tables;
     }
 
+    /**
+     * 获取表的索引
+     *
+     * @param string $table 表名
+     * @param string $database 数据库名称
+     * @return array
+     */
+    public function getTableIndex($table, $database = null) {
+        if (!$database) $database = $this->currentDB;
+        $sql = 'SHOW INDEX FROM '. $database .'.'. $table;
+        $rs = $this->setQuery($sql);
+        $index = array();
+        //Non_unique 0 是唯一 或主键 (0 + Key_name=(PRIMARY))  1 是索引
+        while ($row = $rs->fetch()) {
+            if($row['Key_name'] == 'PRIMARY') {
+                $index['PRIMARY'][] = $row['Column_name'];
+                continue;
+            }
+            if($row['Non_unique'] == '0') {
+                $index['UNIQUE'][$row['Key_name']][] = $row['Column_name'];
+                continue;
+            }
+            $index['INDEX'][$row['Key_name']][] = $row['Column_name'];
+        }
+        return $index;
+    }
+
     /**
      * 获取表的备注
      *

+ 1 - 1
src/Library/Upload.php

@@ -296,7 +296,7 @@ class Upload
      *                        'maxSize' => 1024*1024,
      *                        'maxFolder' => 100);
      */
-    public function upload($filed = 'upload', $configure)
+    public function upload($filed = 'upload', $configure = [])
     {
         $data = array();
         if ($_FILES[$filed]) {