diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index d684fa23..341e0a76 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -121,7 +121,7 @@ class BelongsToMany extends Relation public function getRelation($subRelation = '', $closure = null) { if ($closure) { - call_user_func_array($closure, [& $this->query]); + call_user_func_array($closure, [ & $this->query]); } $result = $this->buildQuery()->relation($subRelation)->select(); $this->hydratePivot($result); @@ -514,7 +514,7 @@ class BelongsToMany extends Relation $changes = [ 'attached' => [], 'detached' => [], - 'updated' => [] + 'updated' => [], ]; $pk = $this->parent->getPk(); $current = $this->pivot->where($this->localKey, $this->parent->$pk) @@ -559,7 +559,7 @@ class BelongsToMany extends Relation */ protected function baseQuery() { - if (empty($this->baseQuery)) { + if (empty($this->baseQuery) && $this->parent->getData()) { $pk = $this->parent->getPk(); $table = $this->pivot->getTable(); $this->query->join($table . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())->where('pivot.' . $this->localKey, $this->parent->$pk); diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 4d9240c5..7d0d5124 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -128,7 +128,7 @@ class HasManyThrough extends Relation */ protected function baseQuery() { - if (empty($this->baseQuery)) { + if (empty($this->baseQuery) && $this->parent->getData()) { $through = $this->through; $model = $this->model; $alias = Loader::parseName(basename(str_replace('\\', '/', $model))); diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index d37af144..a4a2f500 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -273,7 +273,7 @@ class MorphMany extends Relation */ protected function baseQuery() { - if (empty($this->baseQuery)) { + if (empty($this->baseQuery) && $this->parent->getData()) { $pk = $this->parent->getPk(); $map[$this->morphKey] = $this->parent->$pk; $map[$this->morphType] = $this->type; diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index e22198bb..f8124ee3 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -217,7 +217,7 @@ class MorphOne extends Relation */ protected function baseQuery() { - if (empty($this->baseQuery)) { + if (empty($this->baseQuery) && $this->parent->getData()) { $pk = $this->parent->getPk(); $map[$this->morphKey] = $this->parent->$pk; $map[$this->morphType] = $this->type;