From 74315bb894b98cfe0c06e00ab1c689228a1ee6f3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 9 Dec 2016 11:25:24 +0800 Subject: [PATCH] =?UTF-8?q?HasOne=E5=85=B3=E8=81=94=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BD=BF=E7=94=A8hasWhere=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/HasOne.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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); + } }