Skip to content

Commit

Permalink
改进Query类的getTableFields和getFieldsType方法 支持表名自动获取
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 19, 2018
1 parent aaae036 commit 2912353
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions library/think/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,12 @@ public function getConfig($name = '')
* @param string $tableName 数据表名
* @return array
*/
public function getTableFields($tableName)
public function getTableFields($tableName = '')
{
if ('' == $tableName) {
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTable();
}

return $this->connection->getTableFields($tableName);
}

Expand All @@ -449,8 +453,12 @@ public function getTableFields($tableName)
* @param string $field 字段名
* @return array|string
*/
public function getFieldsType($tableName, $field = null)
public function getFieldsType($tableName = '', $field = null)
{
if ('' == $tableName) {
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTable();
}

return $this->connection->getFieldsType($tableName, $field);
}

Expand Down Expand Up @@ -911,11 +919,11 @@ public function field($field, $except = false, $tableName = '', $prefix = '', $a

if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields($tableName ?: (isset($this->options['table']) ? $this->options['table'] : $this->getTable()));
$fields = $this->getTableFields($tableName);
$field = $fields ?: ['*'];
} elseif ($except) {
// 字段排除
$fields = $this->getTableFields($tableName ?: (isset($this->options['table']) ? $this->options['table'] : $this->getTable()));
$fields = $this->getTableFields($tableName);
$field = $fields ? array_diff($fields, $field) : $field;
}

Expand Down

0 comments on commit 2912353

Please sign in to comment.