think\Model::hasWhere PHP Method

hasWhere() public static method

根据关联条件查询当前模型
public static hasWhere ( string $relation, mixed $where = [] ) : Model
$relation string 关联方法名
$where mixed 查询条件(数组或者闭包)
return Model
    public static function hasWhere($relation, $where = [])
    {
        $model = new static();
        $info = $model->{$relation}()->getRelationInfo();
        switch ($info['type']) {
            case Relation::HAS_ONE:
            case Relation::HAS_MANY:
                $table = $info['model']::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.' . $info['localKey'] . '=b.' . $info['foreignKey'], $info['joinType'])->where($where);
            case Relation::HAS_MANY_THROUGH:
                // TODO
            // TODO
            default:
                return $model;
        }
    }