改进关联模型

This commit is contained in:
thinkphp
2016-12-05 11:55:04 +08:00
parent 1d2b44adc8
commit 5037c64f53
9 changed files with 306 additions and 428 deletions

View File

@@ -43,7 +43,10 @@ class MorphMany extends Relation
$this->query = (new $model)->db();
}
// 动态获取关联数据
/**
* 延迟获取关联数据
* @access public
*/
public function getRelation()
{
return $this->select();
@@ -52,23 +55,12 @@ class MorphMany extends Relation
/**
* 预载入关联查询
* @access public
* @param Query $query 查询对象
* @param string $relation 关联名
* @param bool $first 是否需要使用基础表
* @return void
*/
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
{
}
/**
* 预载入关联查询 返回数据集
* @access public
* @param array $resultSet 数据集
* @param string $relation 关联名
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return array
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{
@@ -103,12 +95,14 @@ class MorphMany extends Relation
}
/**
* 预载入关联查询 返回模型对象
* 预载入关联查询
* @access public
* @param Model $result 数据对象
* @param string $relation 关联名
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return Model
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
@@ -147,29 +141,20 @@ class MorphMany extends Relation
return $data;
}
public function __call($method, $args)
/**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{
static $baseQuery = false;
if ($this->query) {
if (empty($baseQuery)) {
$baseQuery = true;
$pk = $this->parent->getPk();
$map[$this->morphKey] = $this->parent->$pk;
$map[$this->morphType] = $this->type;
$this->query->where($map);
}
$result = call_user_func_array([$this->query, $method], $args);
if ($result instanceof \think\db\Query) {
$this->option = $result->getOptions();
return $this;
} else {
$this->option = [];
$baseQuery = false;
return $result;
}
} else {
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
if (empty($this->baseQuery)) {
$pk = $this->parent->getPk();
$map[$this->morphKey] = $this->parent->$pk;
$map[$this->morphType] = $this->type;
$this->query->where($map);
$this->baseQuery = true;
}
}
}