内核更新

This commit is contained in:
2017-02-14 09:41:34 +08:00
parent 689376a5d9
commit b355535009
43 changed files with 330 additions and 100 deletions

View File

@@ -14,7 +14,7 @@ namespace think;
use InvalidArgumentException;
use think\db\Query;
use think\Exception\ValidateException;
use think\model\Collection;
use think\model\Collection as ModelCollection;
use think\model\Relation;
use think\model\relation\BelongsTo;
use think\model\relation\BelongsToMany;
@@ -109,7 +109,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected static $initialized = [];
/**
* 架构函数
* 构造方法
* @access public
* @param array|object $data 数据
*/
@@ -624,7 +624,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/**
* 转换子模型对象
* @access protected
* @param Model|Collection $model
* @param Model|ModelCollection $model
* @param $visible
* @param $hidden
* @param $key
@@ -663,7 +663,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
foreach ($data as $key => $val) {
if ($val instanceof Model || $val instanceof Collection) {
if ($val instanceof Model || $val instanceof ModelCollection) {
// 关联模型对象
$item[$key] = $this->subToArray($val, $visible, $hidden, $key);
} elseif (is_array($val) && reset($val) instanceof Model) {
@@ -712,14 +712,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/**
* 转换当前模型数据集为数据集对象
* @access public
* @param array|Collection $collection 数据集
* @return Collection
* @param array|\think\Collection $collection 数据集
* @return \think\Collection
*/
public function toCollection($collection)
{
if ($this->resultSetType) {
if ('collection' == $this->resultSetType) {
$collection = new Collection($collection);
$collection = new ModelCollection($collection);
} elseif (false !== strpos($this->resultSetType, '\\')) {
$class = $this->resultSetType;
$collection = new $class($collection);
@@ -1081,7 +1081,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 删除条件
$pk = $this->getPk();
if (isset($this->data[$pk])) {
if (is_string($pk) && isset($this->data[$pk])) {
$where = [$pk => $this->data[$pk]];
} elseif (!empty($this->updateWhere)) {
$where = $this->updateWhere;
@@ -1422,15 +1422,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param mixed $operator 比较操作符
* @param integer $count 个数
* @param string $id 关联表的统计字段
* @return Model
* @return Relation|Query
*/
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
{
$model = new static();
$relation = (new static())->$relation();
if (is_array($operator) || $operator instanceof \Closure) {
return $model->$relation()->hasWhere($operator);
return $relation->hasWhere($operator);
}
return $model->$relation()->has($operator, $count, $id);
return $relation->has($operator, $count, $id);
}
/**
@@ -1438,12 +1438,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public
* @param string $relation 关联方法名
* @param mixed $where 查询条件(数组或者闭包)
* @return Model
* @return Relation|Query
*/
public static function hasWhere($relation, $where = [])
{
$model = new static();
return $model->$relation()->hasWhere($where);
return (new static())->$relation()->hasWhere($where);
}
/**
@@ -1483,7 +1482,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$closure = $relation;
$relation = $key;
}
if (strpos($relation, '.')) {
if (is_array($relation)) {
$subRelation = $relation;
$relation = $key;
} elseif (strpos($relation, '.')) {
list($relation, $subRelation) = explode('.', $relation, 2);
}
$method = Loader::parseName($relation, 1, false);
@@ -1509,7 +1511,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$closure = $relation;
$relation = $key;
}
if (strpos($relation, '.')) {
if (is_array($relation)) {
$subRelation = $relation;
$relation = $key;
} elseif (strpos($relation, '.')) {
list($relation, $subRelation) = explode('.', $relation, 2);
}
$relation = Loader::parseName($relation, 1, false);
@@ -1535,7 +1540,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$closure = $relation;
$relation = $key;
}
if (strpos($relation, '.')) {
if (is_array($relation)) {
$subRelation = $relation;
$relation = $key;
} elseif (strpos($relation, '.')) {
list($relation, $subRelation) = explode('.', $relation, 2);
}
$relation = Loader::parseName($relation, 1, false);
@@ -1559,10 +1567,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if ($relation instanceof \Closure) {
$closure = $relation;
$relation = $key;
} elseif (is_string($key)) {
$name = $relation;
$relation = $key;
}
$relation = Loader::parseName($relation, 1, false);
$count = $this->$relation()->relationCount($result, $closure);
$result->setAttr(Loader::parseName($relation) . '_count', $count);
if (!isset($name)) {
$name = Loader::parseName($relation) . '_count';
}
$result->setAttr($name, $count);
}
}