HasOne关联支持使用hasWhere方法

This commit is contained in:
thinkphp
2016-12-09 11:25:24 +08:00
parent 0dab46114c
commit 74315bb894

View File

@@ -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);
}
}