diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 85ac237e..5f75afa4 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -50,4 +50,27 @@ class HasOne extends OneToOne return $this->query->where($this->foreignKey, $this->parent->$localKey)->find(); } + /** + * 根据关联条件查询当前模型 + * @access public + * @param Model $model 模型对象 + * @param mixed $where 查询条件(数组或者闭包) + * @return Query + */ + public function hasWhere($model, $where = []) + { + $table = $this->query->getTable(); + if (is_array($where)) { + foreach ($where as $key => $val) { + if (false === strpos($key, '.')) { + $where['b.' . $key] = $val; + unset($where[$key]); + } + } + } + return $model->db()->alias('a') + ->field('a.*') + ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType) + ->where($where); + } }