关联预载入方法调整 取消class参数 关联的数据集查询根据模型的数据集返回类型设定

This commit is contained in:
thinkphp
2017-01-22 12:42:24 +08:00
parent a7a0051a78
commit 6f02e4b127
12 changed files with 79 additions and 83 deletions

View File

@@ -63,4 +63,5 @@ return [
'route name not exists' => '路由标识不存在(或参数不够)',
'invalid request' => '非法请求',
'bind attr has exists' => '模型的属性已经存在',
'relation data not exists' => '关联数据不存在',
];

View File

@@ -155,6 +155,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->dateFormat = $this->db(false)->getConfig('datetime_format');
}
if (is_null($this->resultSetType)) {
$this->resultSetType = $this->db(false)->getConfig('resultset_type');
}
// 执行初始化操作
$this->initialize();
}
@@ -1404,10 +1407,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public
* @param array $resultSet 数据集
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return array
*/
public function eagerlyResultSet(&$resultSet, $relation, $class = '')
public function eagerlyResultSet(&$resultSet, $relation)
{
$relations = is_string($relation) ? explode(',', $relation) : $relation;
foreach ($relations as $key => $relation) {
@@ -1421,7 +1423,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
list($relation, $subRelation) = explode('.', $relation, 2);
}
$relation = Loader::parseName($relation, 1, false);
$this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class);
$this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure);
}
}
@@ -1430,10 +1432,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public
* @param Model $result 数据对象
* @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return Model
*/
public function eagerlyResult(&$result, $relation, $class = '')
public function eagerlyResult(&$result, $relation)
{
$relations = is_string($relation) ? explode(',', $relation) : $relation;
@@ -1448,7 +1449,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
list($relation, $subRelation) = explode('.', $relation, 2);
}
$relation = Loader::parseName($relation, 1, false);
$this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class);
$this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure);
}
}

View File

@@ -528,11 +528,11 @@ abstract class Connection
}
/**
* 获得数据集
* 获得数据集数组
* @access protected
* @param bool $pdo 是否返回PDOStatement
* @param bool $procedure 是否存储过程
* @return mixed
* @return array
*/
protected function getResult($pdo = false, $procedure = false)
{

View File

@@ -2289,29 +2289,31 @@ class Query
// 数据列表读取后的处理
if (!empty($this->model)) {
// 生成模型对象
$model = $this->model;
$modelName = $this->model;
if (count($resultSet) > 0) {
foreach ($resultSet as $key => $result) {
/** @var Model $result */
$result = new $model($result);
$result->isUpdate(true);
$model = new $modelName($result);
$model->isUpdate(true);
// 关联查询
if (!empty($options['relation'])) {
$result->relationQuery($options['relation']);
$model->relationQuery($options['relation']);
}
// 关联统计
if (!empty($options['with_count'])) {
$result->relationCount($result, $options['with_count']);
$model->relationCount($model, $options['with_count']);
}
$resultSet[$key] = $result;
$resultSet[$key] = $model;
}
if (!empty($options['with'])) {
// 预载入
$result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : '');
$model->eagerlyResultSet($resultSet, $options['with']);
}
// 模型数据集转换
$resultSet = $model->toCollection($resultSet);
} else {
$resultSet = (new $modelName)->toCollection($resultSet);
}
// 模型数据集转换
$resultSet = (new $model)->toCollection($resultSet);
} elseif ('collection' == $this->connection->getConfig('resultset_type')) {
// 返回Collection对象
$resultSet = new Collection($resultSet);
@@ -2406,7 +2408,7 @@ class Query
}
// 预载入查询
if (!empty($options['with'])) {
$data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : '');
$data->eagerlyResult($data, $options['with']);
}
// 关联统计
if (!empty($options['with_count'])) {

View File

@@ -27,8 +27,6 @@ abstract class Relation
protected $foreignKey;
// 关联表主键
protected $localKey;
// 关联查询条件
protected $where;
// 关联查询参数
protected $option;
// 基础查询
@@ -68,12 +66,11 @@ abstract class Relation
* 封装关联数据集
* @access public
* @param array $resultSet 数据集
* @param string $class 数据集类名
* @return mixed
*/
protected function resultSetBuild($resultSet, $class = '')
protected function resultSetBuild($resultSet)
{
return $class ? new $class($resultSet) : $resultSet;
return (new $this->model)->toCollection($resultSet);
}
/**

View File

@@ -11,6 +11,7 @@
namespace think\model\relation;
use think\Exception;
use think\Loader;
use think\Model;
@@ -57,10 +58,9 @@ class BelongsTo extends OneToOne
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class)
protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
@@ -74,8 +74,7 @@ class BelongsTo extends OneToOne
}
if (!empty($range)) {
$this->where[$localKey] = ['in', $range];
$data = $this->eagerlyWhere($this, [
$data = $this->eagerlyWhere($this, [
$localKey => [
'in',
$range,
@@ -85,10 +84,13 @@ class BelongsTo extends OneToOne
$attr = Loader::parseName($relation);
// 关联数据封装
foreach ($resultSet as $result) {
if (!isset($data[$result->$foreignKey])) {
$data[$result->$foreignKey] = [];
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists :' . $this->model);
} else {
$relationModel = $data[$result->$localKey];
}
$relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class);
if (!empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
@@ -106,19 +108,19 @@ class BelongsTo extends OneToOne
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class)
protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
// 关联数据封装
if (!isset($data[$result->$foreignKey])) {
$data[$result->$foreignKey] = [];
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists :' . $this->model);
} else {
$relationModel = $data[$result->$localKey];
}
$relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class);
if (!empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);

View File

@@ -84,10 +84,9 @@ class BelongsToMany extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
@@ -117,7 +116,7 @@ class BelongsToMany extends Relation
$data[$result->$pk] = [];
}
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class));
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk]));
}
}
}
@@ -129,10 +128,9 @@ class BelongsToMany extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
{
$pk = $result->getPk();
if (isset($result->$pk)) {
@@ -144,7 +142,7 @@ class BelongsToMany extends Relation
if (!isset($data[$pk])) {
$data[$pk] = [];
}
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk], $class));
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk]));
}
}

View File

@@ -60,7 +60,7 @@ class HasMany extends Relation
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$range = [];
@@ -72,8 +72,7 @@ class HasMany extends Relation
}
if (!empty($range)) {
$this->where[$this->foreignKey] = ['in', $range];
$data = $this->eagerlyOneToMany($this, [
$data = $this->eagerlyOneToMany($this, [
$this->foreignKey => [
'in',
$range,
@@ -86,7 +85,7 @@ class HasMany extends Relation
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
}
$result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey], $class));
$result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey]));
}
}
}
@@ -101,7 +100,7 @@ class HasMany extends Relation
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
@@ -111,7 +110,7 @@ class HasMany extends Relation
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
}
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class));
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey]));
}
}
@@ -259,9 +258,7 @@ class HasMany extends Relation
protected function baseQuery()
{
if (empty($this->baseQuery)) {
if (isset($this->where)) {
$this->query->where($this->where);
} elseif (isset($this->parent->{$this->localKey})) {
if (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件
$this->query->where($this->foreignKey, $this->parent->{$this->localKey});
}

View File

@@ -11,6 +11,7 @@
namespace think\model\relation;
use think\Exception;
use think\Loader;
use think\Model;
@@ -84,10 +85,9 @@ class HasOne extends OneToOne
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class)
protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
@@ -101,8 +101,7 @@ class HasOne extends OneToOne
}
if (!empty($range)) {
$this->where[$foreignKey] = ['in', $range];
$data = $this->eagerlyWhere($this, [
$data = $this->eagerlyWhere($this, [
$foreignKey => [
'in',
$range,
@@ -112,10 +111,12 @@ class HasOne extends OneToOne
$attr = Loader::parseName($relation);
// 关联数据封装
foreach ($resultSet as $result) {
// 关联模型
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
throw new Exception('relation data not exists : ' . $this->model);
} else {
$relationModel = $data[$result->$localKey];
}
$relationModel = $this->resultSetBuild($data[$result->$localKey], $class);
if (!empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
@@ -133,19 +134,21 @@ class HasOne extends OneToOne
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class)
protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure);
// 关联数据封装
// 关联模型
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
throw new Exception('relation data not exists :' . $this->model);
} else {
$relationModel = $data[$result->$localKey];
}
$relationModel = $this->resultSetBuild($data[$result->$localKey], $class);
if (!empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);

View File

@@ -65,10 +65,9 @@ class MorphMany extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{
$morphType = $this->morphType;
$morphKey = $this->morphKey;
@@ -83,9 +82,7 @@ class MorphMany extends Relation
}
if (!empty($range)) {
$this->where[$morphKey] = ['in', $range];
$this->where[$morphType] = $type;
$data = $this->eagerlyMorphToMany([
$data = $this->eagerlyMorphToMany([
$morphKey => ['in', $range],
$morphType => $type,
], $relation, $subRelation, $closure);
@@ -96,7 +93,7 @@ class MorphMany extends Relation
if (!isset($data[$result->$pk])) {
$data[$result->$pk] = [];
}
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class));
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk]));
}
}
}
@@ -108,15 +105,14 @@ class MorphMany extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
{
$pk = $result->getPk();
if (isset($result->$pk)) {
$data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure);
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class));
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk]));
}
}

View File

@@ -11,6 +11,7 @@
namespace think\model\relation;
use think\Exception;
use think\Loader;
use think\Model;
use think\model\Relation;
@@ -95,10 +96,9 @@ class MorphTo extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{
$morphKey = $this->morphKey;
$morphType = $this->morphType;
@@ -125,10 +125,12 @@ class MorphTo extends Relation
}
foreach ($resultSet as $result) {
if ($key == $result->$morphType) {
// 关联模型
if (!isset($data[$result->$morphKey])) {
$data[$result->$morphKey] = [];
throw new Exception('relation data not exists :' . $this->model);
} else {
$result->setAttr($attr, $data[$result->$morphKey]);
}
$result->setAttr($attr, $this->resultSetBuild($data[$result->$morphKey], $class));
}
}
}
@@ -142,10 +144,9 @@ class MorphTo extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
{
$morphKey = $this->morphKey;
$morphType = $this->morphType;

View File

@@ -100,14 +100,13 @@ abstract class OneToOne extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{
if (1 == $this->eagerlyType) {
// IN查询
$this->eagerlySet($resultSet, $relation, $subRelation, $closure, $class);
$this->eagerlySet($resultSet, $relation, $subRelation, $closure);
} else {
// 模型关联组装
foreach ($resultSet as $result) {
@@ -123,14 +122,13 @@ abstract class OneToOne extends Relation
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
{
if (1 == $this->eagerlyType) {
// IN查询
$this->eagerlyOne($result, $relation, $subRelation, $closure, $class);
$this->eagerlyOne($result, $relation, $subRelation, $closure);
} else {
// 模型关联组装
$this->match($this->model, $relation, $result);