mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
改进多对多查询
This commit is contained in:
@@ -662,7 +662,10 @@ class Relation
|
|||||||
|
|
||||||
public function __call($method, $args)
|
public function __call($method, $args)
|
||||||
{
|
{
|
||||||
|
static $baseQuery = [];
|
||||||
if ($this->query) {
|
if ($this->query) {
|
||||||
|
if (empty($baseQuery[$this->type])) {
|
||||||
|
$baseQuery[$this->type] = true;
|
||||||
switch ($this->type) {
|
switch ($this->type) {
|
||||||
case self::HAS_MANY:
|
case self::HAS_MANY:
|
||||||
if (isset($this->where)) {
|
if (isset($this->where)) {
|
||||||
@@ -686,8 +689,9 @@ class Relation
|
|||||||
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
|
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
|
||||||
break;
|
break;
|
||||||
case self::BELONGS_TO_MANY:
|
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);
|
$result = call_user_func_array([$this->query, $method], $args);
|
||||||
if ($result instanceof \think\db\Query) {
|
if ($result instanceof \think\db\Query) {
|
||||||
|
|||||||
Reference in New Issue
Block a user