朱金辉 1 month ago
parent
commit
fbdc14353a

+ 2 - 1
src/Base/Request.php

@@ -639,7 +639,8 @@ abstract class Request
 
             do {
                 if ($scriptFileName && is_string($scriptFileName)) {
-                    $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));
+                    $suffer = (string) \Qii::getInstance()->appConfigure('ext', '.php');
+                    $fileName = basename($scriptFileName, $suffer);
                     $fileNameLen = strlen($fileName);
 
                     $scriptName = $this->getServer('SCRIPT_NAME');

+ 0 - 2
src/Base/Route.php

@@ -393,7 +393,6 @@ class Route
                 // $route->any('/default/*', ['class', '$2']); 这里的$2值的是request->get(2)这个值
                 if(is_array($callable)) {
                     $action = (isset($callable[1]) ? $callable[1] : Register::get("APP_DEFAULT_ACTION"));
-
                     $controllerCls = _loadClass($callable[0]);
                 }else{
                     if(strpos($callable, ":") > 0) {
@@ -412,7 +411,6 @@ class Route
                     $action = $request->get($key, Register::get("APP_DEFAULT_ACTION"));
                 }
                 $action = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);
-
                 if(preg_match("/^\d/", $action)) {
                     throw new InvalidFormat('方法名不能以数字开头');
                 }

+ 5 - 0
src/Driver/ConnBase.php

@@ -20,6 +20,11 @@ class ConnBase
     protected $_dbInfo;
 
     /**
+     * @var array $_connections 数据库链接
+     */
+    protected $_connections;
+
+    /**
      * @var resource $_writeConnection  获取写的链接
      */
     protected static $_writeConnection = null;

+ 60 - 10
src/Driver/Entity/Base.php

@@ -12,9 +12,25 @@ class Base {
      * @var array $properties
      */
     public static $properties;
-
+    /**
+     * 配置信息
+     *
+     * @var array $config
+     */
     public static $config;
     /**
+     * 唯一字段
+     *
+     * @var array $uniqKeys
+     */
+    public static $uniqKeys = null;
+    /**
+     * 设置排除项
+     *
+     * @var array $exclude
+     */
+    public static $exclude = [];
+    /**
      * @var string 表别名
      */
     private $alias;
@@ -22,7 +38,7 @@ class Base {
      * hooker for fields 查询字段/ where 查询条件 / order 排序 / cache 缓存 / with 关联查询
      * @var array $hooker
      */
-    private $hooker = [];
+    private $hooker = array();
     /**
      * 默认过期时间
      *
@@ -190,24 +206,26 @@ class Base {
     /**
      * 设置order by hooker
      * @param object $hooker
-     * @return void
+     * @return $this
      * @throws \Exception
      */
     public function setOrderHooker($hooker, $args = null) {
         $this->checkCallable($hooker);
         $this->setHooker('order', $hooker, $args);
+        return $this;
     }
 
     /**
      * 设置 limit hooker
      *
      * @param object $hooker
-     * @return void
+     * @return $this
      * @throws \Exception
      */
     public function setLimitHooker($hooker, $args = null) {
         $this->checkCallable($hooker);
         $this->setHooker('order', $hooker, $args);
+        return $this;
     }
     /**
      * 获取limit
@@ -225,12 +243,13 @@ class Base {
      *
      * @param callable $hooker
      * @param array| mixed $args
-     * @return void
+     * @return $this
      * @throws \Exception
      */
     public function setCacheHooker($hooker, $args = null) {
         $this->checkCallable($hooker);
         $this->setHooker('cache', $hooker, $args);
+        return $this;
     }
 
     /**
@@ -376,6 +395,16 @@ class Base {
     {
         return [];
     }
+
+    /**
+     * 设置唯一字段
+     *
+     * @param mixed $uniqKeys 唯一字段
+     * @return void
+     */
+    public function setUniqKeys($uniqKeys) {
+        $this->uniqKeys = $uniqKeys;
+    }
     /**
      * unique (unique 如果是 array 则表示 联合唯一,如果是string则是任意唯一,多个单独唯一使用字符串以逗号隔开, 主键除外)
      *
@@ -383,6 +412,9 @@ class Base {
      * @throws \Exception
      */
     public function uniqueKey(){
+        if(self::$uniqKeys !== null) {
+            return self::$uniqKeys;
+        }
         throw new \Exception('请设置唯一值');
     }
 
@@ -397,11 +429,25 @@ class Base {
     }
 
     /**
-     * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个排除
+     * 设置排除项
+     * @param array $exclude
+     * @return void
+     */
+    public function setExclude($exclude) {
+        if(!is_array($exclude)) {
+            return;
+        }
+        self::$exclude = $exclude;
+    }
+    /**
+     * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个异或排除
      *
      * @return array
      */
     public function exclude(){
+        if(self::$exclude !== null) {
+            return self::$exclude;
+        }
         return array();
     }
 
@@ -614,7 +660,7 @@ class Base {
      *
      * @return array
      */
-    public function toArray()
+    public function toArray($returnFields = false)
     {
         $arr = [];
         $method = new \ReflectionClass($this);
@@ -626,8 +672,10 @@ class Base {
         }
         $fields = $this->fields();
         if(count($fields)) {
-            foreach ($fields as $key => $value) {
-                $arr[$key] = $value;
+            if($returnFields) {
+                foreach ($fields as $key => $value) {
+                    $arr['__fields__'][$key] = $value;
+                }
             }
             foreach ($vars as $extra) {
                 if(!isset($arr[$extra])) {
@@ -1204,9 +1252,11 @@ class Base {
         if(count($this->getFields()) > 0 && !in_array($relKey, $this->getFields())) {
             $fields[] = $relKey;
         }
+        if(array_search('*', $fields) !== false) {
+            $fields = ['*'];
+        }
         $query = $this->createQuery($fields);
         $res = $query->where($args[1])->rs($this->prepareTable());
-        echo $this->calledSQL;
         $rows = [];
         foreach($res as $v) {
             $rows[$v[$relKey]][] = $v;

+ 25 - 8
src/Driver/Entity/Entity.php

@@ -1,6 +1,8 @@
 <?php
 namespace Qii\Driver\Entity;
 
+use entity\SysDictType;
+
 /**
  * 通过数据表生成 entity
  * 使用方法:
@@ -86,28 +88,28 @@ class Entity {
             $comment = strlen($comment) > 0 ? " ". $comment : "";
 
             $doc = [];
-            if(($tableInfo['rules']['int'] && isset($tableInfo['rules']['int'][$field])) ||
-                ($tableInfo['rules']['number'] && isset($tableInfo['rules']['number'][$field]))
+            if(isset($tableInfo['rules']['int'][$field]) ||
+                isset($tableInfo['rules']['number'][$field])
             ) {
                 $doc[] = "\t/**";
                 $doc[] = "\t * @var int ". $convertField . $comment;
                 $doc[] = "\t */";
-            } else if (($tableInfo['rules']['maxlength'] && isset($tableInfo['rules']['maxlength'][$field]))
+            } else if (isset($tableInfo['rules']['maxlength'][$field])
                 || (isset($tableInfo['rules']['text']) && in_array($field, $tableInfo['rules']['text']))
             ) {
                 $doc[] = "\t/**";
                 $doc[] = "\t * @var string ". $convertField . $comment;
                 $doc[] = "\t */";
-            } else if ($tableInfo['rules']['timestamp'] && in_array($field, $tableInfo['rules']['timestamp'])) {
+            } else if (isset($tableInfo['rules']['timestamp']) && in_array($field, $tableInfo['rules']['timestamp'])) {
                 $doc[] = "\t/**";
                 $doc[] = "\t * @var datetime ". $convertField . $comment;
                 $doc[] = "\t */";
-            } else if($tableInfo['rules']['float'] && isset($tableInfo['rules']['float'][$field])) {
+            } else if(isset($tableInfo['rules']['float'][$field])) {
                 $doc[] = "\t/**";
                 $doc[] = "\t * @var float ". $convertField . $comment;
                 $doc[] = "\t */";
             }
-            if($tableInfo['rules']['default'] && isset($tableInfo['rules']['default'][$field])) {
+            if(isset($tableInfo['rules']['default'][$field])) {
                 $default = $tableInfo['rules']['default'][$field];
                 $defaults[$convertField] = $default;
             }
@@ -121,10 +123,25 @@ class Entity {
         $classAndProperty = [];
         $classAndProperty[] = "<?php";
         $classAndProperty[] = "namespace ". $this->namespace . ";";
-        $classAndProperty[] = "\n";
         $classAndProperty[] = "use Qii\Driver\Entity\Inf;";
         $classAndProperty[] = "use Qii\Driver\Entity\Base;";
-        $classAndProperty[] = "\n";
+        $classAndProperty[] = "use Qii\Driver\Response;";
+        $classAndProperty[] = <<<DOC
+/**
+ * @method $class setWhereHooker(\$hooker, \$args = null)
+ * @method $class setWith(\$hooker, \$args = null)
+ * @method $class setOrderHooker(\$hooker, \$args = null)
+ * @method $class setHooker(\$key, \$hooker, \$args)
+ * @method $class setCacheConfig(\$config)
+ * @method $class setCacheHooker(\$hooker, \$args = null)
+ * @method $class setJoinHooker(\$hooker, \$args = null)
+ * @method $class setLimitHooker(\$hooker, \$args = null)
+ * @method $class setOrHooker(\$hooker, \$args = null)
+ * @method $class setQueryFieldsHooker(\$hooker, \$args = null)
+ * @method $class get()
+ * @method mixed|Response info()
+ */
+DOC;
         $classAndProperty[] = 'class '. $class . ' extends Base implements Inf{';
         $dynamicProperty = [];
         foreach ($properties as $property) {

+ 1 - 1
src/Driver/Response.php

@@ -311,7 +311,7 @@ class Response
 	/**
 	 * 返回body中的result,默认返回_result字段
 	 * @param string $key 返回字段
-	 * @return mix
+	 * @return array|mixed
 	 */
 	public function getResult($key = '_result')
 	{