Cml\Db\Base::conditionFactory PHP Метод

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

where 语句组装工厂
public conditionFactory ( string $column, array | integer | string $value, string $operator = '=' )
$column string 如 id user.id (这边的user为表别名如表pre_user as user 这边用user而非带前缀的原表名)
$value array | integer | string
$operator string 操作符
    public function conditionFactory($column, $value, $operator = '=')
    {
        if ($this->sql['where'] == '') {
            $this->sql['where'] = 'WHERE ';
        }
        if ($this->whereNeedAddAndOrOr === 1) {
            $this->sql['where'] .= ' AND ';
        } else {
            if ($this->whereNeedAddAndOrOr === 2) {
                $this->sql['where'] .= ' OR ';
            }
        }
        //下一次where操作默认加上AND
        $this->whereNeedAddAndOrOr = 1;
        if ($operator == 'IN' || $operator == 'NOT IN') {
            empty($value) && ($value = [0]);
            //这边可直接跳过不组装sql,但是为了给用户提示无条件 便于调试还是加上where field in(0)
            $inValue = '(';
            foreach ($value as $val) {
                $inValue .= '%s ,';
                $this->bindParams[] = $val;
            }
            $this->sql['where'] .= "{$column} {$operator} " . rtrim($inValue, ',') . ') ';
        } elseif ($operator == 'BETWEEN' || $operator == 'NOT BETWEEN') {
            $betweenValue = '%s AND %s ';
            $this->bindParams[] = $value[0];
            $this->bindParams[] = $value[1];
            $this->sql['where'] .= "{$column} {$operator} {$betweenValue}";
        } else {
            if ($operator == 'IS NULL' || $operator == 'IS NOT NULL') {
                $this->sql['where'] .= "{$column} {$operator}";
            } else {
                $this->bindParams[] = $value;
                $value = '%s';
                $this->sql['where'] .= "{$column} {$operator} {$value} ";
            }
        }
    }