From 56174d03da932dd52bca362f4aab1d7a06ff8849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E9=87=91=E5=8B=87?= Date: Tue, 5 Nov 2024 08:51:05 +0800 Subject: [PATCH] * [refac] Append field to data. --- lib/base/mao/mao.class.php | 77 +++++++++++++++++++++++++++++++++++--- module/bug/model.php | 2 +- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/lib/base/mao/mao.class.php b/lib/base/mao/mao.class.php index f1091442d3..196a23d9c3 100644 --- a/lib/base/mao/mao.class.php +++ b/lib/base/mao/mao.class.php @@ -243,7 +243,28 @@ class baseMao { $this->conditions = []; - $this->setFields(explode(',', str_replace(' ', '', $fields))); + $fields = explode(',', $fields); + + $alias = []; + foreach($fields as $field) + { + $fieldInfo = explode(' ', trim($field)); + if(count($fieldInfo) == 1) + { + $alias[$fieldInfo[0]] = $fieldInfo[0]; + } + elseif(count($fieldInfo) == 2) + { + $alias[$fieldInfo[0]] = $fieldInfo[1]; + } + else + { + $alias[$fieldInfo[0]] = $fieldInfo[2]; + } + } + + $this->setFields($alias); + return $this; } @@ -310,7 +331,7 @@ class baseMao public function where(string $field) { $this->resetCondition(); - $this->condition['field'] = $field; + $this->condition['field'] = trim($field, '`'); return $this; } @@ -325,7 +346,7 @@ class baseMao public function andWhere(string $field) { $this->resetCondition(); - $this->condition['field'] = $field; + $this->condition['field'] = trim($field, '`'); return $this; } @@ -585,7 +606,10 @@ class baseMao { if(!$this->isConditionMatched($row, $this->conditions)) continue; - return $keyField ? $row->$keyField : $row; + if(!$field) return $row; + + $field = trim($field, '`'); + return $row->$field; } return ''; @@ -632,14 +656,14 @@ class baseMao if(!$this->isConditionMatched($row, $this->conditions)) continue; $data = new stdclass(); - foreach($this->fields as $field) + foreach($this->fields as $field => $alias) { if($field == '*') { $data = $row; break; } - $data->$field = $row->$field; + $data->$alias = $row->$field; } if($keyField) @@ -739,6 +763,47 @@ class baseMao return $this; } + /** + * 获取缓存数据拼接到已有数据。 + * Append cache fields to data. + * + * @param array $data + * @param string $primaryKey + * @access public + * @return void + */ + public function into(array $data, $primaryKey) + { + if(empty($data)) return; + + /* Get data keys as conditions. */ + $keyList = []; + foreach($data as $row) $keyList[$row->$primaryKey] = $row->$primaryKey; + + $rawResult = $this->cache->fetchAll($this->table, $keyList); + + $result = []; + foreach($rawResult as $row) + { + $data = new stdclass(); + foreach($this->fields as $field => $alias) + { + $data->$alias = $row->$field; + } + + if($keyField) + { + $result[$row->$keyField] = $data; + } + else + { + $result[] = $data; + } + } + + return $result; + } + public function __call(string $method, array $args) { $method = strtolower($method); diff --git a/module/bug/model.php b/module/bug/model.php index d6abd8e573..5c7ed9a9e5 100644 --- a/module/bug/model.php +++ b/module/bug/model.php @@ -877,7 +877,7 @@ class bugModel extends model ->page($pager) ->fetchAll(); - $this->mao->use($bugs, 'product')->append(TABLE_PRODUCT, 'name AS productName, shadow'); + $this->mao->select('name AS productName, shadow')->from(TABLE_PRODUCT)->into($bugs, 'product'); return $bugs; }