Procházet zdrojové kódy

update sum total 等字段

zjh před 1 měsícem
rodič
revize
765e35d078

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

@@ -74,7 +74,7 @@ class Base {
      *
      * @var null
      */
-    private $tableName = null;
+    protected $tableName = null;
 
     /**
      * 错误信息
@@ -109,13 +109,14 @@ class Base {
      * 设置表名
      *
      * @param $tableName
-     * @return void
+     * @return $this
      */
     public function setTableName($tableName) {
         if(is_callable($tableName)) {
             $this->tableName = $tableName();
         }
         if($tableName) $this->tableName = $tableName;
+        return $this;
     }
     /**
      * 返回 table 的名字,以便分库分表的时候指定使用
@@ -641,13 +642,13 @@ class Base {
      *
      * @param array|string $fields 字段名称
      * @param $args
-     * @return void
-     * @throws \Exception
+     * @return $this
      */
     final public function field($fields, $args = null) {
         $this->setQueryFieldsHooker(function() use($fields){
             return $fields;
         }, $args);
+        return $this;
     }
 
     /**
@@ -866,6 +867,10 @@ class Base {
         $query->groupBy($this->getGroupBy());
         return $query->selectOne($this->prepareTable());
     }
+
+    final public function sum($field = []){
+
+    }
     /**
      * 检查是否有对应的属性
      *
@@ -1664,6 +1669,21 @@ class Base {
         $this->withList($lists);
         return $lists;
     }
+
+    /**
+     * 获取一行
+     *
+     * @return array|bool
+     * @throws \Exception
+     */
+    final public function row(){
+        $query = $this->createQuery();
+        $query = $query->groupBy($this->getGroupBy());
+        $query = $query->orderBy($this->getOrderBy());
+        $row = $query->selectRow($this->prepareTable());
+        $this->withRow($row);
+        return $row;
+    }
     /**
      * listts
      * @param int $page 页码

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

@@ -211,6 +211,9 @@ DOC;
          * 获取表名
          */
         public function getTable(){
+            if(\$this->tableName != null) {
+                return \$this->tableName;
+            }
             return '{$table}';
         }
 DOC;

+ 4 - 0
src/Driver/Traits/SQL.php

@@ -154,6 +154,10 @@ trait SQL
         $arr = [];
         foreach($fields as $field) {
             // 处理 ['table1.field', 'table2.field']
+            if(preg_match('/(COUNT|SUM)\((.*)\)/', $field)) {
+                $arr[] = $field;
+                continue;
+            }
             $tmp = explode('.', $field);
             if(count($tmp) == 2) {
                 if($tmp[1] == '*') {