改进多对多查询

This commit is contained in:
thinkphp
2016-11-30 17:11:38 +08:00
parent 6f58a26097
commit 61462a628e

View File

@@ -662,7 +662,10 @@ class Relation
public function __call($method, $args)
{
static $baseQuery = [];
if ($this->query) {
if (empty($baseQuery[$this->type])) {
$baseQuery[$this->type] = true;
switch ($this->type) {
case self::HAS_MANY:
if (isset($this->where)) {
@@ -686,8 +689,9 @@ class Relation
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
break;
case self::BELONGS_TO_MANY:
// TODO
$pk = $this->parent->getPk();
$this->query->join($this->middle . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())->where('pivot.' . $this->localKey, $this->parent->$pk);
}
}
$result = call_user_func_array([$this->query, $method], $args);
if ($result instanceof \think\db\Query) {