改进关联多对多的查询 增加getModel方法

This commit is contained in:
thinkphp
2016-12-01 10:50:56 +08:00
parent 099c977b0a
commit a02c7321bb

View File

@@ -21,11 +21,11 @@ class Relation
{ {
const HAS_ONE = 1; const HAS_ONE = 1;
const HAS_MANY = 2; const HAS_MANY = 2;
const HAS_MANY_THROUGH = 5;
const BELONGS_TO = 3; const BELONGS_TO = 3;
const BELONGS_TO_MANY = 4; const BELONGS_TO_MANY = 4;
const MORPH_MANY = 7; const HAS_MANY_THROUGH = 5;
const MORPH_TO = 6; const MORPH_TO = 6;
const MORPH_MANY = 7;
// 父模型对象 // 父模型对象
protected $parent; protected $parent;
@@ -51,6 +51,7 @@ class Relation
protected $where; protected $where;
// 关联查询参数 // 关联查询参数
protected $option; protected $option;
/** /**
* 架构函数 * 架构函数
* @access public * @access public
@@ -61,6 +62,15 @@ class Relation
$this->parent = $model; $this->parent = $model;
} }
/**
* 获取关联的所属模型
* @access public
*/
public function getModel()
{
return $this->parent;
}
/** /**
* 获取当前关联信息 * 获取当前关联信息
* @access public * @access public
@@ -108,7 +118,7 @@ class Relation
// 关联查询 // 关联查询
$pk = $this->parent->getPk(); $pk = $this->parent->getPk();
$condition['pivot.' . $localKey] = $this->parent->$pk; $condition['pivot.' . $localKey] = $this->parent->$pk;
$result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select(); $result = $this->belongsToManyQuery($relation->getModel(), $this->middle, $foreignKey, $localKey, $condition)->select();
foreach ($result as $set) { foreach ($result as $set) {
$pivot = []; $pivot = [];
foreach ($set->getData() as $key => $val) { foreach ($set->getData() as $key => $val) {