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