改进Relation类

This commit is contained in:
thinkphp
2016-04-19 23:18:09 +08:00
parent aa585a60b2
commit dc40fac651

View File

@@ -32,8 +32,6 @@ class Relation
protected $foreignKey; protected $foreignKey;
// 关联键 // 关联键
protected $localKey; protected $localKey;
// 是否预载入
protected $eagerly = false;
/** /**
* 架构函数 * 架构函数
@@ -67,27 +65,45 @@ class Relation
public function getRelation($relation) public function getRelation($relation)
{ {
// 执行关联定义方法 // 执行关联定义方法
$db = $this->parent->$relation(); $model = $this->parent->$relation();
$foreignKey = $this->foreignKey;
$localKey = $this->localKey;
// 判断关联类型执行查询 // 判断关联类型执行查询
switch ($this->type) { switch ($this->type) {
case self::HAS_ONE: case self::HAS_ONE:
case self::BELONGS_TO: case self::BELONGS_TO:
$result = $db->find(); $result = $model::where($foreignKey, $this->parent->$localKey)->find();
if (false === $result) { if (false === $result) {
$class = $this->model; $class = $this->model;
$result = new $class; $result = new $class;
$foreignKey = $this->foreignKey;
$localKey = $this->localKey;
$result->$foreignKey = $this->parent->$localKey; $result->$foreignKey = $this->parent->$localKey;
} }
break; break;
case self::HAS_MANY: case self::HAS_MANY:
$result = $model::where($foreignKey, $this->parent->$localKey)->select();
break;
case self::BELONGS_TO_MANY: case self::BELONGS_TO_MANY:
$result = $db->select(); // 关联查询
$pk = $this->parent->getPk();
$condition['pivot.' . $foreignKey] = $this->parent->$pk;
$result = $this->belongsToManyQuery($this->model, $this->middle, $localKey, $foreignKey, $condition)->select();
foreach ($result as $set) {
$pivot = [];
foreach ($set->toArray() as $key => $val) {
if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key, 2);
if ('pivot' == $name) {
$pivot[$attr] = $val;
unset($set->$key);
}
}
}
$set->pivot = new Pivot($pivot, $this->middle);
}
break; break;
default: default:
// 直接返回 // 直接返回
$result = $db; $result = $model;
} }
return $result; return $result;
} }
@@ -101,8 +117,7 @@ class Relation
*/ */
public function eagerlyResultSet($resultSet, $relation) public function eagerlyResultSet($resultSet, $relation)
{ {
$this->eagerly = true; $relations = is_string($relation) ? explode(',', $relation) : $relation;
$relations = is_string($relation) ? explode(',', $relation) : $relation;
foreach ($relations as $relation) { foreach ($relations as $relation) {
$subRelation = ''; $subRelation = '';
@@ -171,7 +186,6 @@ class Relation
} }
$this->relation = []; $this->relation = [];
} }
$this->eagerly = false;
return $resultSet; return $resultSet;
} }
@@ -184,8 +198,7 @@ class Relation
*/ */
public function eagerlyResult($result, $relation) public function eagerlyResult($result, $relation)
{ {
$this->eagerly = true; $relations = is_string($relation) ? explode(',', $relation) : $relation;
$relations = is_string($relation) ? explode(',', $relation) : $relation;
foreach ($relations as $relation) { foreach ($relations as $relation) {
$subRelation = ''; $subRelation = '';
@@ -229,7 +242,6 @@ class Relation
} }
} }
$this->eagerly = false;
return $result; return $result;
} }
@@ -335,13 +347,8 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
if (!$this->eagerly && isset($this->parent->$localKey)) { // 返回关联的模型对象
// 关联查询封装 return new $model;
return $model::where($foreignKey, $this->parent->$localKey);
} else {
// 预载入封装
return $model;
}
} }
/** /**
@@ -360,13 +367,8 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
if (!$this->eagerly && isset($this->parent->$localKey)) { // 返回关联的模型对象
// 关联查询封装 return new $model;
return $model::where($foreignKey, $this->parent->$localKey);
} else {
// 预载入封装
return $model;
}
} }
/** /**
@@ -385,13 +387,8 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
if (!$this->eagerly && isset($this->parent->$localKey)) { // 返回关联的模型对象
// 关联查询封装 return new $model;
return $model::where($foreignKey, $this->parent->$localKey);
} else {
// 预载入封装
return $model;
}
} }
/** /**
@@ -411,15 +408,9 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->middle = $table; $this->middle = $table;
$pk = $this->parent->getPk();
if (!$this->eagerly && isset($this->parent->$pk)) { // 返回关联的模型对象
// 关联查询 return new $model;
$condition['pivot.' . $foreignKey] = $this->parent->$pk;
return $this->belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition);
} else {
// 预载入封装
return $model;
}
} }
/** /**