Procházet zdrojové kódy

Fixed: ORM count method

朱金辉 před 2 roky
rodič
revize
82535bc8df

+ 0 - 1
demo/private/controller/base.php

@@ -7,7 +7,6 @@ class base extends \Qii\Base\Controller
     public $enableView = true;
     public function indexAction()
     {
-        parent::__construct();
     }
 
 	/**

+ 7 - 0
src/Driver/Base.php

@@ -136,6 +136,13 @@ class Base
      */
     final function selectOne($table)
     {
+        //验证table是否合法
+        if (!is_string($table) || !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}$/", $table)) {
+            if (gettype($table) == 'string') {
+                throw new TableException("表名不能包含怪字符且不能以数字开头");
+            }
+            throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
+        }
         $this->modelSQL = $sql = $this->createSelectSQL($table);
         return $this->getOne($sql);
     }

+ 17 - 0
src/Driver/EasyDrive.php

@@ -368,6 +368,23 @@ class EasyDrive {
     final public function setOrderBy($orderBy) {
         return $this->_easyORM->setOrderBy($orderBy);
     }
+
+    /**
+     * 分组
+     * @param array $groupBy 分组字段数组
+     * @return EasyORM
+     */
+    final public function setGroupBy($groupBy) {
+        return $this->_easyORM->setGroupBy($groupBy);
+    }
+
+    /**
+     * 清除 group by
+     * @return EasyORM
+     */
+    final public function cleanGroupBy() {
+        return $this->_easyORM->cleanGroupBy();
+    }
     /**
      * @return Qii\Driver\Response|mixed
      */

+ 35 - 3
src/Driver/EasyORM.php

@@ -250,6 +250,7 @@ final class EasyORM {
         $this->_easyORM->exclude = array();
         $this->_easyORM->orderBy = array();
         $this->_easyORM->queryFields = '*';
+        $this->_easyORM->groupBy = array();
         return $this;
     }
 
@@ -266,6 +267,7 @@ final class EasyORM {
         $this->cleanExcluded();
         $this->cleanQueryFields();
         $this->cleanOrderBy();
+        $this->cleanGroupBy();
         return $this;
     }
 
@@ -466,10 +468,17 @@ final class EasyORM {
         foreach ($value as $key => $val) {
             $query[$key] = $val;
         }
-        $result = $this->_easyORM->db->fields(' count(1) as count')->limit(1)
+        $countFields = ' COUNT(1) as count';
+        if($this->_easyORM->queryFields != "*") {
+            $countFields = $this->_easyORM->queryFields;
+        }
+        $result = $this->_easyORM->db
+            ->fields($countFields)
+            ->limit(1)
             ->where($query)
             ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
+            ->groupBy($this->_easyORM->groupBy)
             ->selectOne($this->_easyORM->rules->getTable());
         if ($this->_easyORM->db->isError()) {
             return $this->_easyORM->db->getResponse();
@@ -597,6 +606,7 @@ final class EasyORM {
             ->where($fieldsAndValues)
             ->like($this->_easyORM->like)
             ->exclude($this->_easyORM->exclude)
+            ->groupBy($this->_easyORM->groupBy)
             ->orderBy($this->_easyORM->orderBy)
             ->selectRows($this->getTable());
         if ($this->_easyORM->db->isError()) {
@@ -627,8 +637,30 @@ final class EasyORM {
      *
      * @return EasyORM
      */
-    public function cleanOrderBy() {
-        $this->_easyORM->orderBy = [];
+    final public function cleanOrderBy() {
+        $this->_easyORM->orderBy = array();
+        return $this;
+    }
+
+    /**
+     * 设置group by 条件
+     * @param $groupBy
+     * @return $this
+     */
+    final public function setGroupBy($groupBy = array()) {
+        if(!$groupBy) {
+            return $this;
+        }
+        $this->_easyORM->groupBy = $groupBy;
+        return $this;
+    }
+
+    /**
+     * 清除 gorup 条件
+     * @return $this
+     */
+    public function cleanGroupBy() {
+        $this->_easyORM->groupBy = array();
         return $this;
     }
     /**

+ 13 - 0
src/Library/Cookie.php

@@ -45,6 +45,7 @@ class Cookie
 		$crypt->setSecurityKey($this->securityKey);
 		$val = trim($crypt->encrypt(urlencode($val)));
 		setcookie($this->prefix . $name, $val, time() + $expire, '/');
+        return $val;
 	}
 
 	/**
@@ -58,4 +59,16 @@ class Cookie
 		$crypt->setSecurityKey($this->securityKey);
 		return trim(urldecode($crypt->decrypt($val)));
 	}
+
+    /**
+     * decode str
+     * @param $val
+     * @return string
+     */
+    public function decode($val) {
+        if (!$val) return '';
+        $crypt = new \Qii\Library\Crypt();
+        $crypt->setSecurityKey($this->securityKey);
+        return trim(urldecode($crypt->decrypt($val)));
+    }
 }