Skip to content

Commit

Permalink
value和column方法支持json字段和动态获取器
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 6, 2021
1 parent 1cc7f2d commit 6bd5000
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/db/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,26 @@ public function getLastInsID(string $sequence = null)
*/
public function value(string $field, $default = null)
{
return $this->connection->value($this, $field, $default);
$result = $this->connection->value($this, $field, $default);

$array[$field] = $result;
$this->result($array);

return $array[$field];
}

/**
* 得到某个列的数组
* @access public
* @param string|array $field 字段名 多个字段用逗号分隔
* @param string $key 索引
* @param string $key 索引
* @return array
*/
public function column($field, string $key = ''): array
{
return $this->connection->column($this, $field, $key);
$result = $this->connection->column($this, $field, $key);
$this->resultSet($result, false);
return $result;
}

/**
Expand Down Expand Up @@ -619,10 +626,10 @@ public function paginate($listRows = null, $simple = false): Paginator

unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']);

$bind = $this->bind;
$total = $this->count();
$bind = $this->bind;
$total = $this->count();
if ($total > 0) {
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} else {
if (!empty($this->model)) {
$results = new \think\model\Collection([]);
Expand Down
7 changes: 5 additions & 2 deletions src/db/concern/ResultOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ protected function result(array &$result): void
* 处理数据集
* @access public
* @param array $resultSet 数据集
* @param bool $toCollection 是否转为对象
* @return void
*/
protected function resultSet(array &$resultSet): void
protected function resultSet(array &$resultSet, bool $toCollection = true): void
{
if (!empty($this->options['json'])) {
foreach ($resultSet as &$result) {
Expand All @@ -96,7 +97,9 @@ protected function resultSet(array &$resultSet): void
}

// 返回Collection对象
$resultSet = new Collection($resultSet);
if ($toCollection) {
$resultSet = new Collection($resultSet);
}
}

/**
Expand Down

0 comments on commit 6bd5000

Please sign in to comment.