指定查询条件 支持安全过滤
@access public
@param mixed $where 条件表达式
@param mixed $parse 预处理参数
@return Model
339932
public function where($where,$parse=null){
        if(!is_null($parse) && is_string($where)) {
            if(!is_array($parse)) {
                $parse = func_get_args();
                array_shift($parse);
            }
            $parse = array_map(array($this->db,'escapeString'),$parse);
            $where =   vsprintf($where,$parse);
        }elseif(is_object($where)){
            $where  =   get_object_vars($where);
        }
        if(is_string($where) && '' != $where){
            $map    =   array();
            $map['_string']   =   $where;
            $where  =   $map;
        }        
        if(isset($this->options['where'])){
            $this->options['where'] =   array_merge($this->options['where'],$where);
        }else{
            $this->options['where'] =   $where;
        }
        
        return $this;
    }
312928
public function disable($where=null)
    {
        $model = static::getQueryModel();
        
        $deletedField = isset($model['deleted_field']) ? $model['deleted_field'] : (isset($model['fields']['deleted']) ? 'deleted' : null);
        if (!$deletedField) {
            return ErrorInfo::error('deleted filed is not exists.');
        }
        
        $data[] = "$deletedField = ($deletedField + 1) % 2";
        $deleteTimeField = isset($model['fields']['delete_time']) ? 'delete_time' : null;
        if ($deleteTimeField) {
            $data[$deleteTimeField] = time();
        }
        if ($where) {
            $this->getQuery()->where($where);
        }
        $mwhere = $this->getQuery()->get('where');
        
        $res = $this->update()->set($data)->query();
        
        // cache flush current model, 当实际表的操作类不是当前类或和当前类是同一个表的类时才更新，避免了 getQueryModel()和this 是同一个类但实例化的表不一样
        if ($this['table_name'] == $model['table_name']) {
            if (is_numeric($where)) {
                $model->cacheFlushByRecord([$model['primary_key'] => $where]);
            } elseif ($mwhere) {
                $model->cacheFlushByRecord($mwhere);
            }
        }
        
        return ErrorInfo::instance($res === false ? -1 : 0);
    }
723968
public function deleteWhere($where, array $values = [])
    {
        if (!$where instanceof Where) {
            $where = new Where($where, $values);
        }

        $sql = strtr(
            "delete from :relation where :condition returning :projection",
            [
                ':relation'   => $this->getStructure()->getRelation(),
                ':condition'  => (string) $where,
                ':projection' => $this->createProjection()->formatFieldsWithFieldAlias(),
            ]
        );

        $collection = $this->query($sql, $where->getValues());
        foreach ($collection as $entity) {
            $entity->status(FlexibleEntityInterface::STATUS_NONE);
        }
        $collection->rewind();

        return $collection;
    }
1173087
private void setWhere(Where where) throws SqlParseException {
        if (where != null) {
            QueryBuilder whereQuery = QueryMaker.explan(where,this.select.isQuery);
            request.setQuery(whereQuery);
        }
    }
931497
public function where(Expr $where)
    {
        if ($where instanceof Parameter && $where == Expr::true()) {
            return $this;
        }

        if ($where instanceof BinOp && $where->getOperator() === BinOp::AND_) {
            $this->where($where->getLeft());
            $this->where($where->getRight());
        } else {
            $this->where[] = $where;
        }

        return $this;
    }
505651
protected function whereSql() {
        $this->params = [];
        $sql = $this->parseWhere($this->wheres, $this->params);
        if ($sql) {
            $sql = " WHERE " . $sql;
        }
        return $sql;
    }
835495
public function whereOr($expressions = null /**, ...**/)
    {
        if ($expressions instanceof Expression) {
            $expressions = func_get_args();
        }

        return $this->where('$or', array_map(function (Expression $expression) {
            return $expression->toArray();
        }, $expressions));
    }
972373
public function findWhere($where, $columns = ['*'], $or = false)
    {
        $model = $this->query();

        foreach ($where as $field => $value) {

            if ($value instanceof Closure) {

                $model = ( ! $or)
                    ? $model->where($value)
                    : $model->orWhere($value);

            } elseif (is_array($value)) {

                if (count($value) === 3) {

                    list($field, $operator, $search) = $value;

                    $model = ( ! $or)
                        ? $model->where($field, $operator, $search)
                        : $model->orWhere($field, $operator, $search);

                } elseif (count($value) === 2) {

                    list($field, $search) = $value;

                    $model = ( ! $or)
                        ? $model->where($field, $search)
                        : $model->orWhere($field, $search);
                }

            } else {
                $model = ( ! $or)
                    ? $model->where($field, $value)
                    : $model->orWhere($field, $value);
            }
        }

        return $model->get($columns);
    }
1085897
public static function findCreate(array $where) {
        $item = self::find($where);
        if (!$item) {
            $model = get_called_class();
            /* @var $item Record */
            $item = new $model;
            $item->fromArray($where);
        }
        return $item;
    }
923667
private function parseWhere($wheres, &$data) {
        $sql = "";
        foreach ($wheres as $where) {
            if (isset($where[ 'child' ])) {
                if ($sql) {
                    $sql .= " " . $where[ 'logic' ];
                }
                $sql .= " (";
                $sql .= $this->parseWhere($where[ 'child' ], $data);
                $sql .= ")";
            } else {
                if ($sql) {
                    $sql .= " " . $where[ 'logic' ];
                }
                $op = $where[ 'op' ];
                if ($op == 'null') {
                    $op = "is null";
                    $sql .= " " . $where[ 'field' ] . ' ' . $op;
                } else if ($op == 'not null') {
                    $op = "is not null";
                    $sql .= " " . $where[ 'field' ] . ' ' . $op;
                } else if ($op == 'in' || $op == 'not in') {
                    $condition = $where[ 'condition' ];
                    if (!is_array($condition)) {
                        $condition = explode(',', $condition);
                    }
                    $p = [];
                    foreach ($condition as $item) {
                        $p[] .= "?";
                        $data[] = $item;
                    }
                    $p = implode(",", $p);
                    $op .= " (" . $p . ")";
                    $sql .= " " . $where[ 'field' ] . ' ' . $op;
                } else if ($op == 'between' || $op == 'not between') {
                    $sql .= " " . $where[ 'field' ] . ' ' . $op . ' ? and ? ';
                    $data[] = $where[ 'condition' ][ 0 ];
                    $data[] = $where[ 'condition' ][ 1 ];
                } else if ($op == 'day') {
                    if (is_array($where[ 'condition' ])) {
                        $sql .= " " . $where[ 'field' ] . ' ' . 'between' . ' ? and ? ';
                        $data[] = strtotime($where[ 'condition' ][ 0 ]);
                        $data[] = strtotime($where[ 'condition' ][ 1 ]);
                    } else {
                        $sql .= " " . $where[ 'field' ] . ' ' . '>' . ' ?  ';
                        $day = $where[ 'condition' ];
                        $time = strtotime(date("Y-m-d", time()));
                        $data[] = $time - $day * 24 * 60 * 60;
                    }
                } else {
                    if (key_exists($op, Where::$exp)) {
                        $op = Where::$exp[ $op ];
                    }
                    $sql .= " " . $where[ 'field' ] . ' ' . $op;
                    if ($where[ 'condition' ] === 'now') {
                        $sql .= " unix_timestamp(now()) ";
                    } else {
                        $sql .= ' ? ';
                        $data[] = $where[ 'condition' ];
                    }
                }
            }
        }
        return $sql;
    }
1183639
public function doPostDateFiltering($where, $query)
    {
        if (is_admin() || $query->is_main_query()) {
            return $where;
        }

        global $wpdb;

        $from = null;
        $to = null;

        if (isset($_GET[$this->moduleId . 'f']) && !empty($_GET[$this->moduleId . 'f'])) {
            $from = sanitize_text_field($_GET[$this->moduleId . 'f']);
        }

        if (isset($_GET[$this->moduleId . 't']) && !empty($_GET[$this->moduleId . 't'])) {
            $to = sanitize_text_field($_GET[$this->moduleId . 't']);
        }

        if (!is_null($from) && !is_null($to)) {
            $where .= " AND ($wpdb->posts.post_date >= '$from' AND $wpdb->posts.post_date <= '$to')";
        } elseif (!is_null($from) && is_null($to)) {
            $where .= " AND ($wpdb->posts.post_date >= '$from')";
        } elseif (is_null($from) && !is_null($to)) {
            $where .= " AND ($wpdb->posts.post_date <= '$to')";
        }

        return $where;
    }
1087814
public function where($where)
    {
        $where = LambdaUtils::toConditionCallable($where);
        $collection = $this->newInstance();
        foreach($this as $record)
        {
            if(call_user_func($where, $record))
            {
                $collection->add($record);
            }
        }
        return $collection;
    }
1024001
public function where($where, &$params)
    {
        if (!is_array($where)) {
            throw new \Exception("Invalid where clause specified, must be an array");
        }

        $params = Helper::toArray($params);

        $query = "";

        $andFlag = false;

        foreach ($where as $field => $value) {

            # Add the and flag if this isn't the first field
            if ($andFlag) {
                $query .= "AND ";
            } else {
                $andFlag = true;
            }

            # Add the field name to the query
            $query .= $this->quoteField($field);

            # Convert arrays to use the in helper
            if (is_array($value)) {
                $helperMap = [
                    "<"     =>  "lessThan",
                    "<="    =>  "lessThanOrEqualTo",
                    ">"     =>  "greaterThan",
                    ">="    =>  "greaterThanOrEqualTo",
                    "="     =>  "equals",
                    "<>"    =>  "notEqualTo",
                ];
                $clause = (string) reset($value);
                if (count($value) === 2 && isset($helperMap[$clause])) {
                    $method = $helperMap[$clause];
                    $val = next($value);
                    trigger_error("Using arrays for complex where clauses is deprecated in favour of the helper methods, eg Sql::" . $method . "(" . $val . ")", E_USER_DEPRECATED);
                    $value = static::$method($val);
                } else {
                    $value = static::in($value);
                }
            }

            # Any parameters not using a helper should use the standard equals helper
            if (!is_object($value)) {
                $value = static::equals($value);
            }

            $query .= " " . $value->getClause() . " ";
            foreach ($value->getValues() as $val) {
                $params[] = $val;
            }
        }

        return $query;
    }
344128
public function where($where, $regex_constraint = null)
    {
        if (is_array($where)) {
            $other_rule = $where;
        } else {
            $other_rule = [$where => $regex_constraint];
        }

        $this->with = array_merge($this->with, $other_rule);

        return $this;
    }
1113318
public function where(array $where)
    {
        $this->applyCriteria();
        $this->applyScope();

        $this->applyConditions($where);

        return $this;
    }
1201522
protected function parseWhere(){
		$conds = $this->stmt->getConditions();

		// Unset glue on last condition
		unset($conds[count($conds)-1]['glue']);

		$conditions = array();
		foreach($conds as $cond){

			// Conditions with modifiers
			if($cond['simple'] === false){
				$values = $this->escapeValue($cond['values'], $cond['types']);
				$s = '(' . $this->applyModifiers($cond['expr'], $cond['modifiers'], $values) . ')';
				if(isset($cond['glue']))
					$s .= ' ' . $cond['glue'];

				$conditions[] = $s;
				continue;
			}

			// Simple conditions
			$field = $this->parseFieldName($cond['field']);
			$operator = '';
			$value = $cond['value'];
			if($value === null){ // field IS NULL
				$value = ' IS NULL';
			} elseif($value === true){	// field
				$value = '';
			} elseif($value === false){ // NOT field
				$value = $field;
				$field = 'NOT ';
			} elseif($value instanceof Result){
				$operator = ' IN ';
				$value = $this->escapeValue($value, Manager::SUBQUERY);
			} elseif(is_array($value) || $value instanceof Traversable){ // field IN (array)
				$value = ' IN ' . $this->escapeValue($value, Manager::ARR);
			} elseif($value instanceof Literal){ // field = SQL literal
				$operator = ' = ';
				$value = $this->escapeValue($value, Manager::LITERAL);
			} elseif($value instanceof DateTime){ // field = DateTime
				$operator = ' = ';
				$value = $this->escapeValue($value, Manager::DATETIME);
			} else{ // field = value
				$operator = ' = ';
				$value = $this->escapeValue($value);
			}
			$s = '(' . $field . $operator . $value . ')';
			if(isset($cond['glue']))
				$s .= ' '.$cond['glue'];

			$conditions[] = $s;

		}

		return "\nWHERE " . implode(' ', $conditions);
	}
132085
public static function count($where)
     {
         $self = self::instance();
         $rows = new ModelArray();
         //
         $sql = 'select count(*) as cnt from '.$self->DBtable." where $where ";
         $data = Database::read($sql, 1);
         //
         return $data[0]['cnt'];
     }