Skip to content

Commit 0141ea0

Browse files
authored
Merge pull request #39 from xianghuawe/master
增加列,值查询 && before_update 清空oldUpdateData,newUpdateData
2 parents cd38988 + 4edaf36 commit 0141ea0

File tree

1 file changed

+86
-3
lines changed

1 file changed

+86
-3
lines changed

src/think/Model.php

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ protected function _checkTableInfo()
322322
*/
323323
public function flush($tableName = '')
324324
{
325-
if(empty($tableName)){
325+
if (empty($tableName)) {
326326
$tableName = $this->getTableName();
327327
}
328328

329329
if (C('DB_FIELDS_CACHE')) {
330330
$fieldsCache = S('fields_' . strtolower($tableName));
331-
if(!empty($fieldsCache)){
331+
if (!empty($fieldsCache)) {
332332
return $fieldsCache;
333333
}
334334
}
@@ -763,6 +763,8 @@ public function save($data = '', $options = [], $writeEvent = true)
763763
*/
764764
protected function _before_update($pk, &$data, $options, $writeEvent)
765765
{
766+
$this->oldUpdateData = [];
767+
$this->newUpdateData = [];
766768
if ($options["model"] != "EventLog" && $writeEvent) {
767769
$oldData = $this->where($options["where"])->find();
768770
foreach ($data as $key => $value) {
@@ -2424,7 +2426,7 @@ public function getDbFields()
24242426

24252427
$fields = $this->flush($table);
24262428

2427-
if(!empty($fields)){
2429+
if (!empty($fields)) {
24282430
unset($fields['_type'], $fields['_pk']);
24292431
return $fields;
24302432
}
@@ -2779,4 +2781,85 @@ public function setProperty($name, $value)
27792781

27802782
return $this;
27812783
}
2784+
2785+
/**
2786+
* 列查询
2787+
* @param string $field
2788+
* @return array|false|mixed|string
2789+
*/
2790+
public function column(string $field)
2791+
{
2792+
// 分析表达式
2793+
$options = $this->_parseOptions();
2794+
// 标记是列查询
2795+
$options['column_select_field'] = $field;
2796+
// 判断查询缓存
2797+
$key = "";
2798+
if (isset($options['cache'])) {
2799+
$cache = $options['cache'];
2800+
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
2801+
$data = S($key, '', $cache);
2802+
if (false !== $data) {
2803+
return $data;
2804+
}
2805+
}
2806+
$resultSet = $this->db->select($options);
2807+
if (false === $resultSet) {
2808+
return false;
2809+
}
2810+
if (!empty($resultSet)) {
2811+
// 有查询结果
2812+
if (is_string($resultSet)) {
2813+
return $resultSet;
2814+
}
2815+
$resultSet = array_column($resultSet, $field);
2816+
}
2817+
if (isset($cache)) {
2818+
S($key, $resultSet, $cache);
2819+
}
2820+
return $resultSet;
2821+
}
2822+
2823+
/**
2824+
* 值查询
2825+
* @param string $field
2826+
* @return false|mixed|string|null
2827+
*/
2828+
public function value(string $field)
2829+
{
2830+
// 标记是值查询
2831+
$this->options['value_find_field'] = $field;
2832+
// 总是查找一条记录
2833+
$this->options['limit'] = 1;
2834+
// 分析表达式
2835+
$options = $this->_parseOptions();
2836+
// 判断查询缓存
2837+
$key = "";
2838+
if (isset($options['cache'])) {
2839+
$cache = $options['cache'];
2840+
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
2841+
$data = S($key, '', $cache);
2842+
if (false !== $data) {
2843+
$this->data = $data;
2844+
return $data;
2845+
}
2846+
}
2847+
$resultSet = $this->db->select($options);
2848+
if (false === $resultSet) {
2849+
return false;
2850+
}
2851+
if (empty($resultSet)) {
2852+
// 查询结果为空
2853+
return null;
2854+
}
2855+
if (is_string($resultSet)) {
2856+
return $resultSet;
2857+
}
2858+
// 读取数据后的处理
2859+
$this->data = $resultSet[0][$field] ?? null;
2860+
if (isset($cache)) {
2861+
S($key, $this->data, $cache);
2862+
}
2863+
return $this->data;
2864+
}
27822865
}

0 commit comments

Comments
 (0)