改进Relation类的关联定义方法返回对象 统一返回Relation类

This commit is contained in:
thinkphp
2016-07-14 16:34:43 +08:00
parent d38543ca1e
commit b4823a60f3

View File

@@ -43,6 +43,8 @@ class Relation
protected $alias; protected $alias;
// 当前关联的JOIN类型 // 当前关联的JOIN类型
protected $joinType; protected $joinType;
// 关联模型查询对象
protected $query;
/** /**
* 架构函数 * 架构函数
@@ -409,7 +411,7 @@ class Relation
$this->localKey = $localKey; $this->localKey = $localKey;
$this->alias = $alias; $this->alias = $alias;
$this->joinType = $joinType; $this->joinType = $joinType;
$this->query = (new $model)->db();
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
} }
@@ -433,7 +435,7 @@ class Relation
$this->localKey = $otherKey; $this->localKey = $otherKey;
$this->alias = $alias; $this->alias = $alias;
$this->joinType = $joinType; $this->joinType = $joinType;
$this->query = (new $model)->db();
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
} }
@@ -455,7 +457,7 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->alias = $alias; $this->alias = $alias;
$this->query = (new $model)->db();
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
} }
@@ -481,7 +483,7 @@ class Relation
$this->throughKey = $throughKey; $this->throughKey = $throughKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->alias = $alias; $this->alias = $alias;
$this->query = (new $model)->db();
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
} }
@@ -505,7 +507,7 @@ class Relation
$this->localKey = $localKey; $this->localKey = $localKey;
$this->middle = $table; $this->middle = $table;
$this->alias = $alias; $this->alias = $alias;
$this->query = (new $model)->db();
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
} }
@@ -653,14 +655,12 @@ class Relation
public function __call($method, $args) public function __call($method, $args)
{ {
if ($this->model) { if ($this->query) {
$model = new $this->model;
$db = $model->db();
switch ($this->type) { switch ($this->type) {
case self::HAS_MANY: case self::HAS_MANY:
if (isset($this->parent->{$this->localKey})) { if (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件 // 关联查询带入关联条件
$db->where($this->foreignKey, $this->parent->{$this->localKey}); $this->query->where($this->foreignKey, $this->parent->{$this->localKey});
} }
break; break;
case self::HAS_MANY_THROUGH: case self::HAS_MANY_THROUGH:
@@ -671,13 +671,18 @@ class Relation
$pk = (new $this->model)->getPk(); $pk = (new $this->model)->getPk();
$throughKey = $this->throughKey; $throughKey = $this->throughKey;
$modelTable = $this->parent->getTable(); $modelTable = $this->parent->getTable();
$result = $db->field($alias . '.*')->alias($alias) $result = $this->query->field($alias . '.*')->alias($alias)
->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey) ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey) ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey}); ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
break; break;
} }
return call_user_func_array([$db, $method], $args); $result = call_user_func_array([$this->query, $method], $args);
if ($result instanceof \think\db\Query) {
return $this;
} else {
return $result;
}
} else { } else {
throw new Exception('method not exists:' . __CLASS__ . '->' . $method); throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
} }