Think\Db\Builder::buildWhere PHP Метод

buildWhere() публичный Метод

生成查询条件SQL
public buildWhere ( mixed $where, array $options ) : string
$where mixed
$options array
Результат string
    public function buildWhere($where, $options)
    {
        if (empty($where)) {
            $where = [];
        }
        if ($where instanceof Query) {
            return $this->buildWhere($where->getOptions('where'), $options);
        }
        $whereStr = '';
        $binds = $this->query->getFieldsBind($options);
        foreach ($where as $key => $val) {
            $str = [];
            foreach ($val as $field => $value) {
                if ($value instanceof \Closure) {
                    // 使用闭包查询
                    $query = new Query($this->connection);
                    call_user_func_array($value, [&$query]);
                    $str[] = ' ' . $key . ' ( ' . $this->buildWhere($query->getOptions('where'), $options) . ' )';
                } elseif (strpos($field, '|')) {
                    // 不同字段使用相同查询条件(OR)
                    $array = explode('|', $field);
                    $item = [];
                    foreach ($array as $k) {
                        $item[] = $this->parseWhereItem($k, $value, '', $options, $binds);
                    }
                    $str[] = ' ' . $key . ' ( ' . implode(' OR ', $item) . ' )';
                } elseif (strpos($field, '&')) {
                    // 不同字段使用相同查询条件(AND)
                    $array = explode('&', $field);
                    $item = [];
                    foreach ($array as $k) {
                        $item[] = $this->parseWhereItem($k, $value, '', $options, $binds);
                    }
                    $str[] = ' ' . $key . ' ( ' . implode(' AND ', $item) . ' )';
                } else {
                    // 对字段使用表达式查询
                    $field = is_string($field) ? $field : '';
                    $str[] = ' ' . $key . ' ' . $this->parseWhereItem($field, $value, $key, $options, $binds);
                }
            }
            $whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
        }
        return $whereStr;
    }