Kaynağa Gözat

Update: 新增字段检测,字段名必须为大写和下划线开头

zjh 1 hafta önce
ebeveyn
işleme
406c3b6bcb
2 değiştirilmiş dosya ile 35 ekleme ve 25 silme
  1. 34 24
      src/Driver/Entity/Base.php
  2. 1 1
      src/Exceptions/Errors.php

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

@@ -713,7 +713,7 @@ class Base {
         foreach($properties as $property) {
             if($property->isPublic() && !$property->isStatic()) {
                 $name = $property->getName();
-                if(!isset($this->$name)) {
+                if(!isset($this->$name) || !preg_match('/^[A-Z\_].*?/', $name)) {
                     continue;
                 }
                 $field = $this->entity()->convertToField($name);
@@ -913,7 +913,7 @@ class Base {
      *
      * @return array
      */
-    public function toArray($returnFields = false)
+    final public function toArray($returnFields = false)
     {
         $arr = [];
         $method = new \ReflectionClass($this);
@@ -961,12 +961,22 @@ class Base {
         }
         return $arr;
     }
+
+    /**
+     * 将结果转换成json
+     *
+     * @return false|string
+     */
+    final public function toJson() {
+        $arr = $this->toArray();
+        return json_encode($arr, JSON_UNESCAPED_UNICODE);
+    }
     /**
      * 是否相关数据存在
      *
      * @return bool
      */
-    public function exist() {
+    final public function exist() {
         $where = $this->getWhereHooker();
         if(!$where) {
             return false;
@@ -979,7 +989,7 @@ class Base {
      * @return mixed|Qii\Driver\Response
      * @throws \Exception
      */
-    public function add() {
+    final public function add() {
         $valid = $this->validFieldsForAdd();
         if($valid->isError()) {
             return $valid;
@@ -1084,7 +1094,7 @@ class Base {
      * @return mixed | Response
      * @throws \Exception
      */
-    public function update(){
+    final public function update(){
         $valid = $this->validFieldsForUpdate();
         if($valid->isError()) {
             return $valid;
@@ -1152,7 +1162,7 @@ class Base {
      * @return mixed| Response
      * @throws \Exception
      */
-    public function updateFields() {
+    final public function updateFields() {
         $properties = $this->properties();
         $fields = $this->entity()->convertToProperties(array_keys($properties));
         $valid = $this->valid($fields);
@@ -1226,7 +1236,7 @@ class Base {
      * @return mixed|Response
      * @throws \Exception
      */
-    public function incr() {
+    final public function incr() {
         list($where, $or, $exclude, $primary) = $this->condition();
         unset($where, $or, $exclude);
         $property = $this->properties();
@@ -1278,7 +1288,7 @@ class Base {
      *
      * @return false|mixed|string|string[]
      */
-    public function getFields() {
+    final public function getFields() {
         $fields = $this->getFieldsHooker();
         if(!is_array($fields)) {
             $fields = explode(',', preg_replace("/\s{2,}/", " ", preg_replace("/\s(~as)/", "", $fields)));
@@ -1290,7 +1300,7 @@ class Base {
      *
      * @return \Qii\Driver\Base
      */
-    public function createQuery($fields = []) {
+    final public function createQuery($fields = []) {
         if(empty($fields)) {
             $fields = $this->getFields();
         }
@@ -1311,7 +1321,7 @@ class Base {
      * @return mixed|Response
      * @throws \Exception
      */
-    public function first() {
+    final public function first() {
         $orderBy = $this->getOrderBy();
         foreach ($orderBy as $key => $value) {
             $orderBy[$key] = 'ASC';
@@ -1341,7 +1351,7 @@ class Base {
      * @return mixed|Response
      * @throws \Exception
      */
-    public function last() {
+    final public function last() {
         $orderBy = $this->getOrderBy();
         foreach ($orderBy as $key => $value) {
             $orderBy[$key] = 'DESC';
@@ -1373,7 +1383,7 @@ class Base {
      * @param $row
      * @return void
      */
-    public function withRow(&$row) {
+    final public function withRow(&$row) {
         if(!is_array($row)) {
             return;
         }
@@ -1416,7 +1426,7 @@ class Base {
      * @return mixed
      * @throws \Qii\Exceptions\InvalidParams
      */
-    public function rs($page = null, $pageSize = null) {
+    final public function rs($page = null, $pageSize = null) {
         $query = $this->createQuery()->orderBy($this->getOrderBy());
         if($page && $pageSize) {
             $query->limit($page, $pageSize);
@@ -1453,7 +1463,7 @@ class Base {
      * @return array
      * @throws \Exception
      */
-    public function lists($page = 1, $pageSize = 20) {
+    final public function lists($page = 1, $pageSize = 20) {
         $limit = $this->getLimitHooker();
         if(count($limit) > 0) {
             $page = $limit[0];
@@ -1476,7 +1486,7 @@ class Base {
      * @param array $lists
      * @return void
      */
-    public function withList(&$lists) {
+    final public function withList(&$lists) {
         if(!is_array($lists)) {
             return;
         }
@@ -1521,7 +1531,7 @@ class Base {
      * @return array
      * @throws \Exception
      */
-    public function hasOne() {
+    final public function hasOne() {
         $args = func_get_args();
         $relKey = $args[0];
         $fields = [];
@@ -1552,7 +1562,7 @@ class Base {
      * @return array
      * @throws \Exception
      */
-    public function hasMany()
+    final public function hasMany()
     {
         $args = func_get_args();
         $relKey = $args[0];
@@ -1587,7 +1597,7 @@ class Base {
      * @return mixed
      * @throws \Exception
      */
-    public function listAll() {
+    final public function listAll() {
         $limit = $this->getLimitHooker();
         $query = $this->createQuery()->orderBy($this->getOrderBy());
         if(empty($limit) || !is_array($limit)) {
@@ -1612,7 +1622,7 @@ class Base {
      * @param array $fields 字段列表
      * @return Response
      */
-    public function valid($fields = array()) {
+    final public function valid($fields = array()) {
         if(!is_array($fields)) {
             return Response::FailValidate(static::class .'::'. __FUNCTION__,
                 [
@@ -1660,7 +1670,7 @@ class Base {
      * @return mixed|Response
      * @throws \Exception
      */
-    public function valids() {
+    final public function valids() {
         $validArr = func_get_args();
         if(count($validArr) == 0) {
             return Response::Success(static::class .'::'. __FUNCTION__, ['_result' => true]);
@@ -1853,7 +1863,7 @@ class Base {
      *
      * @return mixed|Response
      */
-    public function response() {
+    final public function response() {
         $properties = $this->properties();
         if(!$properties) {
             return Response::Fail(static::class .'::'. __FUNCTION__,
@@ -1918,7 +1928,7 @@ class Base {
      *
      * @return mixed
      */
-    public function startTrans() {
+    final public function startTrans() {
         return $this->db()->transaction();
     }
 
@@ -1927,7 +1937,7 @@ class Base {
      *
      * @return mixed
      */
-    public function commit() {
+    final public function commit() {
         return $this->db()->commit();
     }
 
@@ -1936,7 +1946,7 @@ class Base {
      *
      * @return mixed
      */
-    public function rollback() {
+    final public function rollback() {
         return $this->db()->rollback();
     }
     /**

+ 1 - 1
src/Exceptions/Errors.php

@@ -51,7 +51,7 @@ class Errors extends \Exception
     public static function getError($e)
     {
         //设置状态码
-        $code = !in_array($e->getCode(), array(404, 400, 200)) ? 500 : $e->getCode();
+        $code = !in_array($e->getCode(), array(200, 400, 403, 401, 404)) ? 500 : $e->getCode();
         Factory::getInstance('\Qii\Response\Http')->setResponseCode($code);
 
         $message = array();