修正关联多对多一处错误

This commit is contained in:
thinkphp
2016-12-01 11:28:49 +08:00
parent a02c7321bb
commit 77d2731892

View File

@@ -71,6 +71,15 @@ class Relation
return $this->parent; return $this->parent;
} }
/**
* 获取关联的查询对象
* @access public
*/
public function getQuery()
{
return $this->query;
}
/** /**
* 获取当前关联信息 * 获取当前关联信息
* @access public * @access public
@@ -118,7 +127,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->getModel(), $this->middle, $foreignKey, $localKey, $condition)->select(); $result = $this->belongsToManyQuery($relation->getQuery(), $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) {
@@ -604,7 +613,7 @@ class Relation
// 关联查询封装 // 关联查询封装
$tableName = $model->getTable(); $tableName = $model->getTable();
$relationFk = $model->getPk(); $relationFk = $model->getPk();
return $model::field($tableName . '.*') return $model->field($tableName . '.*')
->field(true, false, $table, 'pivot', 'pivot__') ->field(true, false, $table, 'pivot', 'pivot__')
->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk) ->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk)
->where($condition); ->where($condition);