改进Model类的getPk方法

This commit is contained in:
thinkphp
2016-05-29 11:20:22 +08:00
parent 00cab34945
commit 83e3604314
2 changed files with 12 additions and 9 deletions

View File

@@ -255,15 +255,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
/**
* 获取当前模型对象的主键
* 获取模型对象的主键
* @access public
* @param string $table 数据表
* @param string $name 模型
* @return mixed
*/
public function getPk($table = '')
public function getPk($name = '')
{
if (empty($this->pk)) {
$this->pk = $this->db()->getTableInfo($table, 'pk');
if (!empty($name)) {
$table = $this->db()->getTable($name);
return $this->db()->getPk($table);
} elseif (empty($this->pk)) {
$this->pk = $this->db()->getPk();
}
return $this->pk;
}

View File

@@ -667,9 +667,9 @@ class Query
$alias = $join;
}
$table = !empty($table) ? $table : $this->getTable($join);
if(true === $field){
if (true === $field) {
$fields = $alias . '.*';
}else{
} else {
if (is_string($field)) {
$field = explode(',', $field);
}
@@ -681,7 +681,7 @@ class Query
$fields[] = $alias . '.' . $key . ' AS ' . $val;
$this->options['map'][$val] = $alias . '.' . $key;
}
}
}
}
$this->field($fields);
if ($on) {
@@ -1196,7 +1196,7 @@ class Query
}
/**
* 获取当前模型对象的主键
* 获取当前数据表的主键
* @access public
* @param string $table 数据表名
* @return string|array