From ed3becf64fcf4f7a50177558387bdf9d5aaa9cdc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 10:53:14 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=94=B9=E8=BF=9BHasMany=E5=92=8COneToOne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasMany.php | 2 +- library/think/model/relation/HasOne.php | 15 ++++++++------- library/think/model/relation/OneToOne.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 96688cb1..2ddf370f 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -245,7 +245,7 @@ class HasMany extends Relation } return $this->parent->db()->alias($model) ->field($model . '.*') - ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) + ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey) ->where($where); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index f0b9b9cb..339a271e 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -54,24 +54,25 @@ class HasOne extends OneToOne /** * 根据关联条件查询当前模型 * @access public - * @param Model $model 模型对象 * @param mixed $where 查询条件(数组或者闭包) * @return Query */ - public function hasWhere($model, $where = []) + public function hasWhere($where = []) { - $table = $this->query->getTable(); + $table = $this->query->getTable(); + $model = basename(str_replace('\\', '/', get_class($this->parent))); + $relation = basename(str_replace('\\', '/', $this->model)); if (is_array($where)) { foreach ($where as $key => $val) { if (false === strpos($key, '.')) { - $where['b.' . $key] = $val; + $where[$relation . '.' . $key] = $val; unset($where[$key]); } } } - return $model->db()->alias('a') - ->field('a.*') - ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + return $this->parent->db()->alias($model) + ->field($model . '.*') + ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) ->where($where); } diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ea16b92b..e7d7f0b6 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -26,6 +26,18 @@ abstract class OneToOne extends Relation // 要绑定的属性 protected $bindAttr = []; + /** + * 设置join类型 + * @access public + * @param string $type JOIN类型 + * @return $this + */ + public function joinType($type) + { + $this->joinType = $type; + return $this; + } + /** * 预载入关联查询(JOIN方式) * @access public From a3fd00eb3594c0f78edd51fedfe4357e03b0ba4d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 10:55:26 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index dc745b7f..a7e5dda2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1386,7 +1386,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected function getForeignKey($name) { if (strpos($name, '\\')) { - $name = basename(str_replace('\\', '/', $model)); + $name = basename(str_replace('\\', '/', $name)); } return Loader::parseName($name) . '_id'; } From fea1b5958235d5880400c68e1d67f3e9715ff99e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 11:42:12 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=94=B9=E8=BF=9Bcookie=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper.php b/helper.php index 367e827f..d74f3d58 100644 --- a/helper.php +++ b/helper.php @@ -330,7 +330,7 @@ if (!function_exists('cookie')) { Cookie::clear($value); } elseif ('' === $value) { // 获取 - return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name); + return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name, $option); } elseif (is_null($value)) { // 删除 return Cookie::delete($name); From a1cb6ea4cabab32a38cb881e2e4d99ad9c93fb0b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 Jan 2017 13:45:51 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasOne.php | 2 +- library/think/model/relation/OneToOne.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 339a271e..b1fa25f0 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -40,7 +40,7 @@ class HasOne extends OneToOne * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($subRelation = '', $colsure = null) + public function getRelation($subRelation = '', $closure = null) { // 执行关联定义方法 $localKey = $this->localKey; diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index e7d7f0b6..63903a47 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -19,7 +19,7 @@ use think\model\Relation; abstract class OneToOne extends Relation { - // 预载入方式 + // 预载入方式 0 -JOIN 1 -IN protected $eagerlyType = 0; // 当前关联的JOIN类型 protected $joinType; @@ -79,8 +79,7 @@ abstract class OneToOne extends Relation if ($closure) { // 执行闭包查询 call_user_func_array($closure, [ & $query]); - //指定获取关联的字段 - //需要在 回调中 调方法 withField 方法,如 + // 使用withField指定获取关联的字段,如 // $query->where(['id'=>1])->withField('id,name'); if ($query->getOptions('with_field')) { $field = $query->getOptions('with_field'); From be6d528f68773dab4f5d552bf0480c8b71d518dc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 10:25:10 +0800 Subject: [PATCH 5/8] =?UTF-8?q?collection=E5=8A=A9=E6=89=8B=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=BF=94=E5=9B=9Ethink\Collection=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/model/relation/MorphMany.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/helper.php b/helper.php index d74f3d58..d5946fb2 100644 --- a/helper.php +++ b/helper.php @@ -14,6 +14,7 @@ //------------------------- use think\Cache; +use think\Collection; use think\Config; use think\Cookie; use think\Db; @@ -23,7 +24,6 @@ use think\exception\HttpResponseException; use think\Lang; use think\Loader; use think\Log; -use think\model\Collection; use think\Request; use think\Response; use think\Session; diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index c4c23df7..68eb0834 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -160,6 +160,7 @@ class MorphMany extends Relation * @param array $where 关联预查询条件 * @param string $relation 关联名 * @param string $subRelation 子关联 + * @param \Closure $closure 闭包 * @return array */ protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false) From 4c2665b9f9d1b8bd4b6d3d354f88b971f5b8edc9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:49:50 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E9=A2=84?= =?UTF-8?q?=E8=BD=BD=E5=85=A5=E6=9F=A5=E8=AF=A2=E9=BB=98=E8=AE=A4=E6=94=B9?= =?UTF-8?q?=E4=B8=BAIN=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 63903a47..da989798 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -20,7 +20,7 @@ use think\model\Relation; abstract class OneToOne extends Relation { // 预载入方式 0 -JOIN 1 -IN - protected $eagerlyType = 0; + protected $eagerlyType = 1; // 当前关联的JOIN类型 protected $joinType; // 要绑定的属性 From 4a7e4d5d28b8f051a834436c4f194f51700377c2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:56:41 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E9=A2=84?= =?UTF-8?q?=E8=BD=BD=E5=85=A5=20In=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=85=BC=E5=AE=B9withField=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index da989798..69f57072 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -267,6 +267,10 @@ abstract class OneToOne extends Relation // 预载入关联查询 支持嵌套预载入 if ($closure) { call_user_func_array($closure, [ & $model]); + if ($model->getOptions('with_field')) { + $model->field($model->getOptions('with_field')); + $model->removeOption('with_field'); + } } $list = $model->where($where)->with($subRelation)->select(); From cffb772743a94d803dd3223189f39acdb878f4fb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 Jan 2017 13:58:27 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/OneToOne.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 69f57072..0ff739cc 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -267,9 +267,8 @@ abstract class OneToOne extends Relation // 预载入关联查询 支持嵌套预载入 if ($closure) { call_user_func_array($closure, [ & $model]); - if ($model->getOptions('with_field')) { - $model->field($model->getOptions('with_field')); - $model->removeOption('with_field'); + if ($field = $model->getOptions('with_field')) { + $model->field($field)->removeOption('with_field'); } } $list = $model->where($where)->with($subRelation)->select();