改进关联模型

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

@@ -11,6 +11,8 @@
namespace think\model; namespace think\model;
use think\db\Query;
abstract class Relation abstract class Relation
{ {
// 父模型对象 // 父模型对象
@@ -31,6 +33,8 @@ abstract class Relation
protected $where; protected $where;
// 关联查询参数 // 关联查询参数
protected $option; protected $option;
// 基础查询
protected $baseQuery;
/** /**
* 获取关联的所属模型 * 获取关联的所属模型
@@ -77,12 +81,16 @@ abstract class Relation
public function __call($method, $args) public function __call($method, $args)
{ {
if ($this->query) { if ($this->query) {
// 执行基础查询
$this->baseQuery();
$result = call_user_func_array([$this->query, $method], $args); $result = call_user_func_array([$this->query, $method], $args);
if ($result instanceof \think\db\Query) { if ($result instanceof Query) {
$this->option = $result->getOptions(); $this->option = $result->getOptions();
return $this; return $this;
} else { } else {
$this->option = []; $this->option = [];
$this->baseQuery = false;
return $result; return $result;
} }
} else { } else {

View File

@@ -11,13 +11,10 @@
namespace think\model\relation; namespace think\model\relation;
use think\Db;
use think\db\Query;
use think\Loader;
use think\Model; use think\Model;
use think\model\Relation; use think\model\relation\OneToOne;
class BelongsTo extends Relation class BelongsTo extends OneToOne
{ {
/** /**
* 架构函数 * 架构函数
@@ -40,7 +37,10 @@ class BelongsTo extends Relation
$this->query = (new $model)->db(); $this->query = (new $model)->db();
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
@@ -48,124 +48,4 @@ class BelongsTo extends Relation
return $this->query->where($localKey, $this->parent->$foreignKey)->find(); return $this->query->where($localKey, $this->parent->$foreignKey)->find();
} }
/**
* 预载入关联查询
* @access public
* @param Query $query 查询对象
* @param string $relation 关联名
* @return void
*/
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
{
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
if ($first) {
$table = $query->getTable();
$query->table([$table => $alias]);
if ($query->getOptions('field')) {
$field = $query->getOptions('field');
$query->removeOption('field');
} else {
$field = true;
}
$query->field($field, false, $table, $alias);
}
// 预载入封装
$joinTable = $this->query->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
$query->via($joinAlias);
$query->join($joinTable . ' ' . $joinAlias, $alias . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType);
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $query]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) {
$field = $query->getOptions('with_field');
$query->removeOption('with_field');
}
} elseif (isset($this->option['field'])) {
$field = $this->option['field'];
} else {
$field = true;
}
$query->field($field, false, $joinTable, $joinAlias, $relation . '__');
}
/**
* 预载入关联查询 返回数据集
* @access public
* @param array $resultSet 数据集
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return array
*/
public function eagerlyResultSet(&$resultSet, $relation)
{
foreach ($resultSet as $result) {
// 模型关联组装
$this->match($this->model, $relation, $result);
}
}
/**
* 预载入关联查询 返回模型对象
* @access public
* @param Model $result 数据对象
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return Model
*/
public function eagerlyResult(&$result, $relation)
{
// 模型关联组装
$this->match($this->model, $relation, $result);
}
/**
* 一对一 关联模型预查询拼装
* @access public
* @param string $model 模型名称
* @param string $relation 关联名
* @param Model $result 模型对象实例
* @return void
*/
protected function match($model, $relation, &$result)
{
// 重新组装模型数据
foreach ($result->getData() as $key => $val) {
if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
}
}
}
$result->setAttr($relation, !isset($list[$relation]) ? null : (new $model($list[$relation]))->isUpdate(true));
}
/**
* 保存(新增)当前关联数据对象
* @access public
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @return integer
*/
public function save($data)
{
if ($data instanceof Model) {
$data = $data->getData();
}
// 保存关联表数据
$data[$this->foreignKey] = $this->parent->{$this->localKey};
$model = new $this->model;
return $model->save($data);
}
} }

View File

@@ -43,7 +43,10 @@ class BelongsToMany extends Relation
$this->query = (new $model)->db(); $this->query = (new $model)->db();
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
@@ -70,25 +73,14 @@ class BelongsToMany 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 * @access public
* @param array $resultSet 数据集 * @param array $resultSet 数据集
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return array * @return void
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{ {
@@ -125,12 +117,14 @@ class BelongsToMany extends Relation
} }
/** /**
* 预载入关联查询 返回模型对象 * 预载入关联查询(单个数据)
* @access public * @access public
* @param Model $result 数据对象 * @param Model $result 数据对象
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return Model * @return void
*/ */
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{ {
@@ -304,27 +298,18 @@ class BelongsToMany extends Relation
} }
} }
public function __call($method, $args) /**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{ {
static $baseQuery = false; if (empty($this->baseQuery)) {
if ($this->query) { $pk = $this->parent->getPk();
if (empty($baseQuery)) { $this->query->join($this->middle . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())->where('pivot.' . $this->localKey, $this->parent->$pk);
$baseQuery = true; $this->baseQuery = true;
$pk = $this->parent->getPk();
$this->query->join($this->middle . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())->where('pivot.' . $this->localKey, $this->parent->$pk);
}
$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);
} }
} }
} }

View File

@@ -37,7 +37,10 @@ class HasMany extends Relation
$this->query = (new $model)->db(); $this->query = (new $model)->db();
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
return $this->select(); return $this->select();
@@ -46,23 +49,12 @@ class HasMany extends Relation
/** /**
* 预载入关联查询 * 预载入关联查询
* @access public * @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 array $resultSet 数据集
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return array * @return void
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{ {
@@ -97,12 +89,14 @@ class HasMany extends Relation
} }
/** /**
* 预载入关联查询 返回模型对象 * 预载入关联查询
* @access public * @access public
* @param Model $result 数据对象 * @param Model $result 数据对象
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return Model * @return void
*/ */
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{ {
@@ -221,31 +215,22 @@ class HasMany extends Relation
->where($where); ->where($where);
} }
public function __call($method, $args) /**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{ {
static $baseQuery = false; if (empty($this->baseQuery)) {
if ($this->query) { if (isset($this->where)) {
if (empty($baseQuery)) { $this->query->where($this->where);
$baseQuery = true; } elseif (isset($this->parent->{$this->localKey})) {
if (isset($this->where)) { // 关联查询带入关联条件
$this->query->where($this->where); $this->query->where($this->foreignKey, $this->parent->{$this->localKey});
} elseif (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件
$this->query->where($this->foreignKey, $this->parent->{$this->localKey});
}
} }
$this->baseQuery = true;
$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);
} }
} }
} }

View File

@@ -45,7 +45,10 @@ class HasManyThrough extends Relation
$this->query = (new $model)->db(); $this->query = (new $model)->db();
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
return $this->select(); return $this->select();
@@ -65,12 +68,14 @@ class HasManyThrough extends Relation
} }
/** /**
* 预载入关联查询 返回数据集 * 预载入关联查询
* @access public * @access public
* @param array $resultSet 数据集 * @param array $resultSet 数据集
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return array * @return void
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{ {
@@ -81,45 +86,38 @@ class HasManyThrough extends Relation
* 预载入关联查询 返回模型对象 * 预载入关联查询 返回模型对象
* @access public * @access public
* @param Model $result 数据对象 * @param Model $result 数据对象
* @param string $relation 关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组 * @param string $class 数据集对象名 为空表示数组
* @return Model * @return void
*/ */
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{ {
} }
public function __call($method, $args) /**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{ {
static $baseQuery = false; if (empty($this->baseQuery)) {
if ($this->query) { $through = $this->middle;
if (empty($baseQuery)) { $model = $this->model;
$baseQuery = true; $alias = Loader::parseName(basename(str_replace('\\', '/', $model)));
$through = $this->middle; $throughTable = $through::getTable();
$model = $this->model; $pk = (new $this->model)->getPk();
$alias = Loader::parseName(basename(str_replace('\\', '/', $model))); $throughKey = $this->throughKey;
$throughTable = $through::getTable(); $modelTable = $this->parent->getTable();
$pk = (new $this->model)->getPk(); $this->query->field($alias . '.*')->alias($alias)
$throughKey = $this->throughKey; ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
$modelTable = $this->parent->getTable(); ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
$this->query->field($alias . '.*')->alias($alias) ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey) $this->baseQuery = true;
->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
}
$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);
} }
} }
} }

View File

@@ -11,13 +11,10 @@
namespace think\model\relation; namespace think\model\relation;
use think\Db;
use think\db\Query;
use think\Loader;
use think\Model; use think\Model;
use think\model\Relation; use think\model\relation\OneToOne;
class HasOne extends Relation class HasOne extends OneToOne
{ {
/** /**
* 架构函数 * 架构函数
@@ -40,7 +37,10 @@ class HasOne extends Relation
$this->query = (new $model)->db(); $this->query = (new $model)->db();
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
// 执行关联定义方法 // 执行关联定义方法
@@ -50,125 +50,4 @@ class HasOne extends Relation
return $this->query->where($this->foreignKey, $this->parent->$localKey)->find(); return $this->query->where($this->foreignKey, $this->parent->$localKey)->find();
} }
/**
* 预载入关联查询
* @access public
* @param Query $query 查询对象
* @param string $relation 关联名
* @return void
*/
public function eagerly(Query $query, $relation, $subRelation, &$closure, $first)
{
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
if ($first) {
$table = $query->getTable();
$query->table([$table => $alias]);
if ($query->getOptions('field')) {
$field = $query->getOptions('field');
$query->removeOption('field');
} else {
$field = true;
}
$query->field($field, false, $table, $alias);
}
// 预载入封装
$joinTable = $this->query->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
$query->via($joinAlias);
$query->join($joinTable . ' ' . $joinAlias, $alias . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType);
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $query]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) {
$field = $query->getOptions('with_field');
$query->removeOption('with_field');
}
$closure = null;
} elseif (isset($this->option['field'])) {
$field = $this->option['field'];
} else {
$field = true;
}
$query->field($field, false, $joinTable, $joinAlias, $relation . '__');
}
/**
* 预载入关联查询 返回数据集
* @access public
* @param array $resultSet 数据集
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return array
*/
public function eagerlyResultSet(&$resultSet, $relation)
{
foreach ($resultSet as $result) {
// 模型关联组装
$this->match($this->model, $relation, $result);
}
}
/**
* 预载入关联查询 返回模型对象
* @access public
* @param Model $result 数据对象
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return Model
*/
public function eagerlyResult(&$result, $relation)
{
// 模型关联组装
$this->match($this->model, $relation, $result);
}
/**
* 一对一 关联模型预查询拼装
* @access public
* @param string $model 模型名称
* @param string $relation 关联名
* @param Model $result 模型对象实例
* @return void
*/
protected function match($model, $relation, &$result)
{
// 重新组装模型数据
foreach ($result->getData() as $key => $val) {
if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
}
}
}
$result->setAttr($relation, !isset($list[$relation]) ? null : (new $model($list[$relation]))->isUpdate(true));
}
/**
* 保存(新增)当前关联数据对象
* @access public
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @return integer
*/
public function save($data)
{
if ($data instanceof Model) {
$data = $data->getData();
}
// 保存关联表数据
$data[$this->foreignKey] = $this->parent->{$this->localKey};
$model = new $this->model;
return $model->save($data);
}
} }

View File

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

View File

@@ -11,7 +11,6 @@
namespace think\model\relation; namespace think\model\relation;
use think\db\Query;
use think\Loader; use think\Loader;
use think\Model; use think\Model;
use think\model\Relation; use think\model\Relation;
@@ -38,7 +37,10 @@ class MorphTo extends Relation
$this->alias = $alias; $this->alias = $alias;
} }
// 动态获取关联数据 /**
* 延迟获取关联数据
* @access public
*/
public function getRelation() public function getRelation()
{ {
$morphKey = $this->morphKey; $morphKey = $this->morphKey;
@@ -70,19 +72,6 @@ class MorphTo extends Relation
return $model; return $model;
} }
/**
* 预载入关联查询
* @access public
* @param Query $query 查询对象
* @param string $relation 关联名
* @param bool $first 是否需要使用基础表
* @return void
*/
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
{
}
/** /**
* 预载入关联查询 返回数据集 * 预载入关联查询 返回数据集
* @access public * @access public
@@ -163,4 +152,12 @@ class MorphTo extends Relation
$result->setAttr($relation, $data ?: null); $result->setAttr($relation, $data ?: null);
} }
/**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{
}
} }

View File

@@ -0,0 +1,161 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\model\relation;
use think\db\Query;
use think\Loader;
use think\Model;
use think\model\Relation;
use think\model\relation\BelongsTo;
abstract class OneToOne extends Relation
{
/**
* 预载入关联查询
* @access public
* @param Query $query 查询对象
* @param string $relation 关联名
* @param string $subRelation 子关联
* @param \Closure $closure 闭包条件
* @param bool $first
* @return void
*/
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
{
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
if ($first) {
$table = $query->getTable();
$query->table([$table => $alias]);
if ($query->getOptions('field')) {
$field = $query->getOptions('field');
$query->removeOption('field');
} else {
$field = true;
}
$query->field($field, false, $table, $alias);
}
// 预载入封装
$joinTable = $this->query->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
$query->via($joinAlias);
if ($this instanceof BelongsTo) {
$query->join($joinTable . ' ' . $joinAlias, $alias . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType);
} else {
$query->join($joinTable . ' ' . $joinAlias, $alias . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType);
}
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $query]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) {
$field = $query->getOptions('with_field');
$query->removeOption('with_field');
}
} elseif (isset($this->option['field'])) {
$field = $this->option['field'];
} else {
$field = true;
}
$query->field($field, false, $joinTable, $joinAlias, $relation . '__');
}
/**
* 预载入关联查询
* @access public
* @param array $resultSet 数据集
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation)
{
foreach ($resultSet as $result) {
// 模型关联组装
$this->match($this->model, $relation, $result);
}
}
/**
* 预载入关联查询 返回模型对象
* @access public
* @param Model $result 数据对象
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation)
{
// 模型关联组装
$this->match($this->model, $relation, $result);
}
/**
* 一对一 关联模型预查询拼装
* @access public
* @param string $model 模型名称
* @param string $relation 关联名
* @param Model $result 模型对象实例
* @return void
*/
protected function match($model, $relation, &$result)
{
// 重新组装模型数据
foreach ($result->getData() as $key => $val) {
if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
}
}
}
$result->setAttr($relation, !isset($list[$relation]) ? null : (new $model($list[$relation]))->isUpdate(true));
}
/**
* 保存(新增)当前关联数据对象
* @access public
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @return integer
*/
public function save($data)
{
if ($data instanceof Model) {
$data = $data->getData();
}
// 保存关联表数据
$data[$this->foreignKey] = $this->parent->{$this->localKey};
$model = new $this->model;
return $model->save($data);
}
/**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{
}
}