From 14993a7d9c40f068c7613f2e947bfbc0e14281d3 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Fri, 3 Mar 2017 11:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=BD=BDBelongsToMany=E7=9A=84select?= =?UTF-8?q?=20find=20paginate=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think/model/relation/BelongsToMany.php | 98 +++++++++++++++---- 1 file changed, 78 insertions(+), 20 deletions(-) diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index acf945f6..53d80f65 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -11,12 +11,14 @@ namespace think\model\relation; +use think\Collection; use think\db\Query; use think\Exception; use think\Loader; use think\Model; use think\model\Pivot; use think\model\Relation; +use think\Paginator; class BelongsToMany extends Relation { @@ -67,6 +69,42 @@ class BelongsToMany extends Relation return new $pivot($this->parent, $data, $this->middle); } + /** + * 合成中间表模型 + * @param array|Collection|Paginator $models + */ + protected function hydratePivot($models) + { + foreach ($models as $model) { + $pivot = []; + foreach ($model->getData() as $key => $val) { + if (strpos($key, '__')) { + list($name, $attr) = explode('__', $key, 2); + if ('pivot' == $name) { + $pivot[$attr] = $val; + unset($model->$key); + } + } + } + $model->pivot = $this->newPivot($pivot); + } + } + + /** + * 创建关联查询Query对象 + * @return Query + */ + protected function buildQuery() + { + $foreignKey = $this->foreignKey; + $localKey = $this->localKey; + $middle = $this->middle; + // 关联查询 + $pk = $this->parent->getPk(); + $condition['pivot.' . $localKey] = $this->parent->$pk; + return $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition); + } + /** * 延迟获取关联数据 * @param string $subRelation 子关联名 @@ -75,29 +113,49 @@ class BelongsToMany extends Relation */ public function getRelation($subRelation = '', $closure = null) { - $foreignKey = $this->foreignKey; - $localKey = $this->localKey; - $middle = $this->middle; if ($closure) { call_user_func_array($closure, [& $this->query]); } - // 关联查询 - $pk = $this->parent->getPk(); - $condition['pivot.' . $localKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->relation($subRelation)->select(); - foreach ($result as $set) { - $pivot = []; - foreach ($set->getData() as $key => $val) { - if (strpos($key, '__')) { - list($name, $attr) = explode('__', $key, 2); - if ('pivot' == $name) { - $pivot[$attr] = $val; - unset($set->$key); - } - } - } - $set->pivot = $this->newPivot($pivot); - } + $result = $this->buildQuery()->relation($subRelation)->select(); + $this->hydratePivot($result); + return $result; + } + + /** + * 重载select方法 + * @param null $data + * @return false|\PDOStatement|string|Collection + */ + public function select($data = null) + { + $result = $this->buildQuery()->select($data); + $this->hydratePivot($result); + return $result; + } + + /** + * 重载paginate方法 + * @param null $listRows + * @param bool $simple + * @param array $config + * @return Paginator + */ + public function paginate($listRows = null, $simple = false, $config = []) + { + $result = $this->buildQuery()->paginate($listRows, $simple, $config); + $this->hydratePivot($result); + return $result; + } + + /** + * 重载find方法 + * @param null $data + * @return array|false|\PDOStatement|string|Model + */ + public function find($data = null) + { + $result = $this->buildQuery()->find($data); + $this->hydratePivot([$result]); return $result; }