CDbCriteria represents a query criteria, such as conditions, ordering by,
limit/offset.
It can be used in AR query methods such as CActiveRecord::find and
CActiveRecord::findAll.
$criteria=new CDbCriteria();
$criteria->compare('status',Post::STATUS_ACTIVE);
$criteria->addInCondition('id',array(1,2,3,4,5,6));
$posts = Post::model()->findAll($criteria);
Methods summary
public
|
#
__construct( array $data = array() )
Constructor.
Parameters
- $data
array $data criteria initial property values (indexed by property name)
|
public
|
#
__wakeup( )
Remaps criteria parameters on unserialize to prevent name collisions.
Remaps criteria parameters on unserialize to prevent name collisions.
Since
1.1.9
|
public
static
|
#
addCondition( mixed $condition, string $operator = 'AND' )
Appends a condition to the existing CDbCriteria::$condition . The new condition and
the existing condition will be concatenated via the specified operator which
defaults to 'AND'. The new condition can also be an array. In this case, all
elements in the array will be concatenated together via the operator. This
method handles the case when the existing condition is empty. After calling this
method, the CDbCriteria::$condition property will be modified.
Appends a condition to the existing CDbCriteria::$condition . The new condition and
the existing condition will be concatenated via the specified operator which
defaults to 'AND'. The new condition can also be an array. In this case, all
elements in the array will be concatenated together via the operator. This
method handles the case when the existing condition is empty. After calling this
method, the CDbCriteria::$condition property will be modified.
Parameters
- $condition
mixed $condition the new condition. It can be either a string or an array of strings.
- $operator
string $operator the operator to join different conditions. Defaults to 'AND'.
Returns
static the criteria object itself
|
public
static
|
#
addSearchCondition( string $column, string $keyword, boolean $escape = true, string $operator = 'AND', string $like = 'LIKE' )
Appends a search condition to the existing CDbCriteria::$condition . The search
condition and the existing condition will be concatenated via the specified
operator which defaults to 'AND'. The search condition is generated using the
SQL LIKE operator with the given column name and search keyword.
Appends a search condition to the existing CDbCriteria::$condition . The search
condition and the existing condition will be concatenated via the specified
operator which defaults to 'AND'. The search condition is generated using the
SQL LIKE operator with the given column name and search keyword.
Parameters
- $column
string $column the column name (or a valid SQL expression)
- $keyword
string $keyword the search keyword. This interpretation of the keyword is affected by
the next parameter.
- $escape
boolean $escape whether the keyword should be escaped if it 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
keyword will be surrounded with a % character on both ends. When this parameter
is false, the keyword will be directly used for matching without any change.
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
- $like
string $like the LIKE operator. Defaults to 'LIKE'. You may also set this to be 'NOT
LIKE'.
Returns
static the criteria object itself
|
public
static
|
#
addInCondition( string $column, array $values, string $operator = 'AND' )
Appends an IN condition to the existing CDbCriteria::$condition . The IN condition
and the existing condition will be concatenated via the specified operator which
defaults to 'AND'. The IN condition is generated by using the SQL IN operator
which requires the specified column value to be among the given list of
values.
Appends an IN condition to the existing CDbCriteria::$condition . The IN condition
and the existing condition will be concatenated via the specified operator which
defaults to 'AND'. The IN condition is generated by using the SQL IN operator
which requires the specified column value to be among the given list of
values.
Parameters
- $column
string $column the column name (or a valid SQL expression)
- $values
array $values list of values that the column value should be in
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
Returns
static the criteria object itself
|
public
static
|
#
addNotInCondition( string $column, array $values, string $operator = 'AND' )
Appends an NOT IN condition to the existing CDbCriteria::$condition . The NOT IN
condition and the existing condition will be concatenated via the specified
operator which defaults to 'AND'. The NOT IN condition is generated by using the
SQL NOT IN operator which requires the specified column value to be among the
given list of values.
Appends an NOT IN condition to the existing CDbCriteria::$condition . The NOT IN
condition and the existing condition will be concatenated via the specified
operator which defaults to 'AND'. The NOT IN condition is generated by using the
SQL NOT IN operator which requires the specified column value to be among the
given list of values.
Parameters
- $column
string $column the column name (or a valid SQL expression)
- $values
array $values list of values that the column value should not be in
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
Returns
static the criteria object itself
Since
1.1.1
|
public
static
|
#
addColumnCondition( array $columns, string $columnOperator = 'AND', string $operator = 'AND' )
Appends a condition for matching the given list of column values. The
generated condition will be concatenated to the existing CDbCriteria::$condition via
the specified operator which defaults to 'AND'. The condition is generated by
matching each column and the corresponding value.
Appends a condition for matching the given list of column values. The
generated condition will be concatenated to the existing CDbCriteria::$condition via
the specified operator which defaults to 'AND'. The condition is generated by
matching each column and the corresponding value.
Parameters
- $columns
array $columns list of column names and values to be matched (name=>value)
- $columnOperator
string $columnOperator the operator to concatenate multiple column matching condition.
Defaults to 'AND'.
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
Returns
static the criteria object itself
|
public
static
|
#
compare( string $column, mixed $value, boolean $partialMatch = false, string $operator = 'AND', boolean $escape = true )
Adds a comparison expression to the CDbCriteria::$condition property.
This method is a helper that appends to the CDbCriteria::$condition property with a
new comparison expression. The comparison is done by comparing a column with the
given value using some comparison operator.
The comparison operator is intelligently determined based on the first few
characters in the given value. In particular, it recognizes the following
operators if they appear as the leading characters in the given value:
< : 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.
Parameters
- $column
string $column the name of the column to be searched
- $value
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.
- $partialMatch
boolean $partialMatch whether the value should consider partial text match (using LIKE
and NOT LIKE operators). Defaults to false, meaning exact comparison.
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
- $escape
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.
Returns
static the criteria object itself
Since
1.1.1
|
public
static
|
#
addBetweenCondition( string $column, string $valueStart, string $valueEnd, string $operator = 'AND' )
Adds a between condition to the CDbCriteria::$condition property.
The new between condition and the existing condition will be concatenated via
the specified operator which defaults to 'AND'. If one or both values are empty
then the condition is not added to the existing condition. This method handles
the case when the existing condition is empty. After calling this method, the
CDbCriteria::$condition property will be modified.
Parameters
- $column
string $column the name of the column to search between.
- $valueStart
string $valueStart the beginning value to start the between search.
- $valueEnd
string $valueEnd the ending value to end the between search.
- $operator
string $operator the operator used to concatenate the new condition with the existing
one. Defaults to 'AND'.
Returns
static the criteria object itself
Since
1.1.2
|
public
|
#
mergeWith( mixed $criteria, string|boolean $operator = 'AND' )
Merges with another criteria. In general, the merging makes the resulting
criteria more restrictive. For example, if both criterias have conditions, they
will be 'AND' together. Also, the criteria passed as the parameter takes
precedence in case two options cannot be merged (e.g. LIMIT, OFFSET).
Merges with another criteria. In general, the merging makes the resulting
criteria more restrictive. For example, if both criterias have conditions, they
will be 'AND' together. Also, the criteria passed as the parameter takes
precedence in case two options cannot be merged (e.g. LIMIT, OFFSET).
Parameters
- $criteria
mixed $criteria the criteria to be merged with. Either an array or CDbCriteria.
- $operator
string|boolean $operator the operator used to concatenate where and having conditions. Defaults
to 'AND'. For backwards compatibility a boolean value can be passed: - 'false'
for 'OR' - 'true' for 'AND'
|
public
array
|
#
toArray( )
Returns
array the array representation of the criteria
|
Properties summary
public static
integer
|
$paramCount
|
0 |
#
the global counter for anonymous binding parameters. This counter is used for
generating the name for the anonymous parameters.
the global counter for anonymous binding parameters. This counter is used for
generating the name for the anonymous parameters.
|
public
mixed
|
$select
|
'*' |
#
the columns being selected. This refers to the SELECT clause in an SQL
statement. The property can be either a string (column names separated by
commas) or an array of column names. Defaults to '*', meaning all columns.
the columns being selected. This refers to the SELECT clause in an SQL
statement. The property can be either a string (column names separated by
commas) or an array of column names. Defaults to '*', meaning all columns.
|
public
boolean
|
$distinct
|
false |
#
whether to select distinct rows of data only. If this is set true, the SELECT
clause would be changed to SELECT DISTINCT.
whether to select distinct rows of data only. If this is set true, the SELECT
clause would be changed to SELECT DISTINCT.
|
public
string
|
$condition
|
'' |
#
query condition. This refers to the WHERE clause in an SQL statement. For
example, age><span class="php-num">31</span> <span
class="php-keyword1">AND</span> team=<span
class="php-num">1</span> .
query condition. This refers to the WHERE clause in an SQL statement. For
example, age><span class="php-num">31</span> <span
class="php-keyword1">AND</span> team=<span
class="php-num">1</span> .
|
public
array
|
$params
|
array() |
#
list of query parameter values indexed by parameter placeholders. For
example, <span class="php-keyword1">array</span>(<span
class="php-quote">':name'</span>=><span
class="php-quote">'Dan'</span>, <span
class="php-quote">':age'</span>=><span
class="php-num">31</span>) .
list of query parameter values indexed by parameter placeholders. For
example, <span class="php-keyword1">array</span>(<span
class="php-quote">':name'</span>=><span
class="php-quote">'Dan'</span>, <span
class="php-quote">':age'</span>=><span
class="php-num">31</span>) .
|
public
integer
|
$limit
|
-1 |
#
maximum number of records to be returned. If less than 0, it means no
limit.
maximum number of records to be returned. If less than 0, it means no
limit.
|
public
integer
|
$offset
|
-1 |
#
zero-based offset from where the records are to be returned. If less than 0,
it means starting from the beginning.
zero-based offset from where the records are to be returned. If less than 0,
it means starting from the beginning.
|
public
string
|
$order
|
'' |
#
how to sort the query results. This refers to the ORDER BY clause in an SQL
statement.
how to sort the query results. This refers to the ORDER BY clause in an SQL
statement.
|
public
string
|
$group
|
'' |
#
how to group the query results. This refers to the GROUP BY clause in an SQL
statement. For example, <span class="php-quote">'projectID,
teamID'</span> .
how to group the query results. This refers to the GROUP BY clause in an SQL
statement. For example, <span class="php-quote">'projectID,
teamID'</span> .
|
public
string
|
$join
|
'' |
#
how to join with other tables. This refers to the JOIN clause in an SQL
statement. For example, <span class="php-quote">'LEFT JOIN users ON
users.id=authorID'</span> .
how to join with other tables. This refers to the JOIN clause in an SQL
statement. For example, <span class="php-quote">'LEFT JOIN users ON
users.id=authorID'</span> .
|
public
string
|
$having
|
'' |
#
the condition to be applied with GROUP-BY clause. For example, <span
class="php-quote">'SUM(revenue)<50000'</span> .
the condition to be applied with GROUP-BY clause. For example, <span
class="php-quote">'SUM(revenue)<50000'</span> .
|
public
mixed
|
$with
|
|
#
the relational query criteria. This is used for fetching related objects in
eager loading fashion. This property is effective only when the criteria is
passed as a parameter to the following methods of CActiveRecord:
The property value will be used as the parameter to the CActiveRecord::with() method to perform the eager loading. Please refer to
CActiveRecord::with() on how to specify this parameter.
the relational query criteria. This is used for fetching related objects in
eager loading fashion. This property is effective only when the criteria is
passed as a parameter to the following methods of CActiveRecord:
The property value will be used as the parameter to the CActiveRecord::with() method to perform the eager loading. Please refer to
CActiveRecord::with() on how to specify this parameter.
Since
1.1.0
|
public
string
|
$alias
|
|
#
the alias name of the table. If not set, it means the alias is 't'.
the alias name of the table. If not set, it means the alias is 't'.
|
public
boolean
|
$together
|
|
#
whether the foreign tables should be joined with the primary table in a
single SQL. This property is only used in relational AR queries for HAS_MANY and
MANY_MANY relations.
When this property is set true, only a single SQL will be executed for a
relational AR query, even if the primary table is limited and the relationship
between a foreign table and the primary table is many-to-one.
When this property is set false, a SQL statement will be executed for each
HAS_MANY relation.
When this property is not set, if the primary table is limited or paginated,
a SQL statement will be executed for each HAS_MANY relation. Otherwise, a single
SQL statement will be executed for all.
whether the foreign tables should be joined with the primary table in a
single SQL. This property is only used in relational AR queries for HAS_MANY and
MANY_MANY relations.
When this property is set true, only a single SQL will be executed for a
relational AR query, even if the primary table is limited and the relationship
between a foreign table and the primary table is many-to-one.
When this property is set false, a SQL statement will be executed for each
HAS_MANY relation.
When this property is not set, if the primary table is limited or paginated,
a SQL statement will be executed for each HAS_MANY relation. Otherwise, a single
SQL statement will be executed for all.
Since
1.1.4
|
public
string
|
$index
|
|
#
the name of the AR attribute whose value should be used as index of the query
result array. Defaults to null, meaning the result array will be zero-based
integers.
the name of the AR attribute whose value should be used as index of the query
result array. Defaults to null, meaning the result array will be zero-based
integers.
Since
1.1.5
|
public
mixed
|
$scopes
|
|
#
scopes to apply
This property is effective only when passing criteria to the one of the
following methods:
Can be set to one of the following:
- One scope: $criteria->scopes='scopeName';
- Multiple scopes: $criteria->scopes=array('scopeName1','scopeName2');
- Scope with parameters:
$criteria->scopes=array('scopeName'=>array($params));
- Multiple scopes with parameters:
$criteria->scopes=array('scopeName1'=>array($params1),'scopeName2'=>array($params2));
- Multiple scopes with the same name:
array(array('scopeName'=>array($params1)),array('scopeName'=>array($params2)));
scopes to apply
This property is effective only when passing criteria to the one of the
following methods:
Can be set to one of the following:
- One scope: $criteria->scopes='scopeName';
- Multiple scopes: $criteria->scopes=array('scopeName1','scopeName2');
- Scope with parameters:
$criteria->scopes=array('scopeName'=>array($params));
- Multiple scopes with parameters:
$criteria->scopes=array('scopeName1'=>array($params1),'scopeName2'=>array($params2));
- Multiple scopes with the same name:
array(array('scopeName'=>array($params1)),array('scopeName'=>array($params2)));
Since
1.1.7
|