*
<: the column must be less than the given value.
* >: the column must be greater than the given value.
* <=: the column must be less than or equal to the given value.
* >=: the column must be greater than or equal to the given value.
* <>: the column must not be the same as the given value.
* Note that when $partialMatch is true, this would mean the value must not be a substring
* of the column.
* =: the column must be equal to the given value.
* none of the above: the column must be equal to the given value. Note that when $partialMatch
* is true, this would mean the value must be the same as the given value or be a substring of it.
*
*
* Note that any surrounding white spaces will be removed from the value before comparison.
* When the value is empty, no comparison expression will be added to the search condition.
*
* @param string $column the name of the column to be searched
* @param mixed $value the column value to be compared with. If the value is a string, the aforementioned
* intelligent comparison will be conducted. If the value is an array, the comparison is done
* by exact match of any of the value in the array. If the string or the array is empty,
* the existing search condition will not be modified.
* @param boolean $partialMatch whether the value should consider partial text match (using LIKE and NOT LIKE operators).
* Defaults to false, meaning exact comparison.
* @param string $operator the operator used to concatenate the new condition with the existing one.
* Defaults to 'AND'.
* @param boolean $escape whether the value should be escaped if $partialMatch is true and
* the value contains characters % or _. When this parameter is true (default),
* the special characters % (matches 0 or more characters)
* and _ (matches a single character) will be escaped, and the value will be surrounded with a %
* character on both ends. When this parameter is false, the value will be directly used for
* matching without any change.
* @return static the criteria object itself
* @since 1.1.1
*/
public function compare($column, $value, $partialMatch = false, $operator = 'AND', $escape = true): static
{
if (is_array($value)) {
if ($value === array()) {
return $this;
}
return $this->addInCondition($column, $value, $operator);
} else {
$value = "$value";
}
if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=|%|\!=))?(.*)$/', $value, $matches)) {
$value = $matches[2];
$op = $matches[1];
} else {
$op = '';
}
if ($op == '%') {
//模糊匹配
$partialMatch = true;
$op = '';
}
if ($value === '') {
return $this;
}
if ($partialMatch) {
if ($op === '') {
return $this->addSearchCondition($column, $value, $escape, $operator);
}
if ($op === '<>') {
return $this->addSearchCondition($column, $value, $escape, $operator, 'NOT LIKE');
}
} elseif ($op === '') {
$op = '=';
}
$this->addCondition($column.$op.self::PARAM_PREFIX.self::$paramCount, $operator);
$this->params[self::PARAM_PREFIX.self::$paramCount++] = $value;
return $this;
}
/**
* 设置 分页数据
* @param int $page
* @param int $pageSize
* @return DbCriteria
*/
public function setPage(int $page = 1, int $pageSize = 30): static
{
if ($page < 0 || $pageSize < 1) {
return $this;
}
$this->pageSize = $this->limit = $pageSize;
$this->page = $page > 0 ? $page : 1;
if ($this->offset > 0) {
$this->offset += ($this->page - 1) * $this->limit;
} else {
$this->offset = ($this->page - 1) * $this->limit;
}
$this->_fenye = 1;
return $this;
}
public function setLockStatus($status): static
{
$this->lock = (bool)$status;
return $this;
}
public function getLockStatus(): bool
{
return $this->lock;
}
public function getPage(): int
{
return $this->page;
}
public function getPageSize(): int
{
return $this->pageSize;
}
public function setSelect($field = '*'): static
{
$this->select = $field;
return $this;
}
public function setJoin($join): static
{
$this->join = $join;
return $this;
}
public function addJoin($join): static
{
$this->join.= ' ' . $join;
return $this;
}
public function setAlias($alias): static
{
$this->alias = $alias;
return $this;
}
public function setGroup($group): static
{
$this->group = $group;
return $this;
}
public function setOrder($order): static
{
$this->order = $order;
return $this;
}
public function addOrder($order): static
{
if ($this->order) {
$this->order .= ','.$order;
} else {
$this->order = $order;
}
return $this;
}
public function setOffset($offset): static
{
$this->offset = $offset;
return $this;
}
public function addOffset($offset): static
{
$this->offset += $offset;
return $this;
}
public function setLimit($limit): static
{
$this->limit = $limit;
return $this;
}
public function setHaving($having): static
{
$this->having = $having;
return $this;
}
public function andHaving($having): static
{
if (!$having) {
return $this;
}
if (!$this->having) {
$this->having = $having;
} else {
$this->having .= ' AND '.$having;
}
return $this;
}
public function orHaving($having): static
{
if (!$having) {
return $this;
}
if (!$this->having) {
$this->having = $having;
} else {
$this->having .= ' OR '.$having;
}
return $this;
}
public function setForceIndex($index): static
{
$this->forceIndex = $index;
return $this;
}
/**
* 开启线上日志记录到指定时间
* @param string $tag 标记 查找日志方便
* @param string $deadline 必须指定 -1永久(不建议使用)
*/
public function setDebugUntil($tag, $deadline): static
{
if ($deadline != '-1' && strtotime($deadline) < time()) {
$this->debugMode = self::DEBUG_OFF;
return $this;
}
$this->debugTag = $tag;
$this->debugMode = self::DEBUG_ON;
return $this;
}
public function getDebugMode(): bool
{
return $this->debugMode;
}
/**
* @return array the array representation of the criteria
*/
public function toArray(): array
{
$result = array();
foreach (
[
'select',
// 'condition',
// 'params',
'limit',
'offset',
'order',
'group',
'join',
'having',
'distinct',
'scopes',
'with',
'alias',
'index',
'together',
'page',
'pageSize',
'debugTag',
] as $name
) {
$result[$name] = $this->$name;
}
return $result;
}
public function getSql($sql, $params): string
{
if ($params) {
foreach ($params as $k => $v) {
if (!is_numeric($v)) {
$params[$k] = "'{$v}'";
}
}
// 这里倒序会导致 :ycp5 覆盖 :ycp50
$params = array_reverse($params);
$sql = str_replace(array_keys($params), array_values($params), $sql);
$sql = str_replace("\n", ' ', $sql);
}
return $this->getLockStatus() ? $sql." FOR UPDATE" : $sql;
}
public function getDebugTag(): string
{
return $this->debugTag;
}
public function getUseTime()
{
}
public function isFenye(): bool
{
return $this->_fenye;
}
/**
* 分页情况下,查询总条数的 select 字段,如果含有 group 时可能需要额外插叙字段
* @param $str
* @return string
*/
public function setCountSelectStr($str): string
{
return $this->countSelect .= ','.trim($str, ',');
}
public function getCountSelectStr(): string
{
return $this->countSelect;
}
/**
* 简单的compare
* @param array $filter
* @param bool $checkPage
* @return $this
*/
public static function simpleCompare(array $filter): static
{
$cri = new static();
foreach ($filter as $k => $v) {
$cri->compare($k, $v);
}
return $cri;
}
/**
* 简单的compare
* @param array $filter
* @param bool $checkPage
* @return $this
*/
public static function simpleCompareWithPage(array $filter): static
{
$cri = self::simpleCompare($filter);
$cri->setPage(
Helper::getArrParam($_REQUEST, 'current', 'int', 1),
Helper::getArrParam($_REQUEST, 'size', 'int', 20)
);
return $cri;
}
}