Class CSort
CSort represents information relevant to sorting.
When data needs to be sorted according to one or several attributes, we can use CSort to represent the sorting information and generate appropriate hyperlinks that can lead to sort actions.
CSort is designed to be used together with CActiveRecord
. When
creating a CSort instance, you need to specify CSort::$modelClass
. You can use
CSort to generate hyperlinks by calling CSort::link()
. You can also use CSort to
modify a CDbCriteria
instance by calling CSort::applyOrder()
so that it
can cause the query results to be sorted according to the specified
attributes.
In order to prevent SQL injection attacks, CSort ensures that only valid
model attributes can be sorted. This is determined based on CSort::$modelClass
and CSort::$attributes
. When CSort::$attributes
is not set, all attributes
belonging to CSort::$modelClass
can be sorted. When CSort::$attributes
is set,
only those attributes declared in the property can be sorted.
By configuring CSort::$attributes
, one can perform more complex sorts that
may consist of things like compound attributes (e.g. sort based on the
combination of first name and last name of users).
The property CSort::$attributes
should be an array of key-value pairs, where
the keys represent the attribute names, while the values represent the virtual
attribute definitions. For more details, please check the documentation about
CSort::$attributes
.
- CComponent
- CSort
Direct known subclasses
SmartSortCopyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Located at x2engine/framework/web/CSort.php
public
|
|
public
|
#
applyOrder(
Modifies the query criteria by changing its |
public
string
|
|
public
string
|
|
public
string
|
#
resolveLabel( string $attribute )
Resolves the attribute label for the specified attribute. This will invoke
|
public
array
|
|
public
mixed
|
#
getDirection( string $attribute )
Returns the sort direction of the specified attribute in the current request. |
public
string
|
#
createUrl(
Creates a URL that can lead to generating sorted data. |
public
mixed
|
|
protected
|
|
protected
string
|
#
createLink( string $attribute, string $label, string $url, array $htmlOptions )
Creates a hyperlink based on the given label and URL. You may override this method to customize the link generation. |
boolean |
SORT_ASC
|
false |
#
Sort ascending |
boolean |
SORT_DESC
|
true |
#
Sort descending |
public
boolean
|
$multiSort | false |
#
whether the sorting can be applied to multiple attributes simultaneously. Defaults to false, which means each time the data can only be sorted by one attribute. |
public
string
|
$modelClass |
|
#
the name of the model class whose attributes can be sorted. The model class
must be a child class of |
public
array
|
$attributes | array() |
#
list of attributes that are allowed to be sorted. For example,
array('user_id','create_time') would specify that only 'user_id' and
'create_time' of the model This property can also be used to specify complex sorting. To do so, a virtual attribute can be declared in terms of a key-value pair in the array. The key refers to the name of the virtual attribute that may appear in the sort request, while the value specifies the definition of the virtual attribute. In the simple case, a key-value pair can be like A more flexible way is to specify the key-value pair as 'user'=>array( 'asc'=>'first_name, last_name', 'desc'=>'first_name DESC, last_name DESC', 'label'=>'Name' ) where 'user' is the name of the virtual attribute that specifies the full name of user (a compound attribute consisting of first name and last name of user). In this case, we have to use an array to define the virtual attribute with three elements: 'asc', 'desc' and 'label'. The above approach can also be used to declare virtual attributes that consist of relational attributes. For example, 'price'=>array( 'asc'=>'item.price', 'desc'=>'item.price DESC', 'label'=>'Item Price' ) Note, the attribute name should not contain '-' or '.' characters because
they are used as Starting from version 1.1.3, an additional option named 'default' can be used in the virtual attribute declaration. This option specifies whether an attribute should be sorted in ascending or descending order upon user clicking the corresponding sort hyperlink if it is not currently sorted. The valid option values include 'asc' (default) and 'desc'. For example, 'price'=>array( 'asc'=>'item.price', 'desc'=>'item.price DESC', 'label'=>'Item Price', 'default'=>'desc', ) Also starting from version 1.1.3, you can include a star ('*') element in this property so that all model attributes are available for sorting, in addition to those virtual attributes. For example, 'attributes'=>array( 'price'=>array( 'asc'=>'item.price', 'desc'=>'item.price DESC', 'label'=>'Item Price', 'default'=>'desc', ), '*', ) Note that when a name appears as both a model attribute and a virtual attribute, the position of the star element in the array determines which one takes precedence. In particular, if the star element is the first element in the array, the model attribute takes precedence; and if the star element is the last one, the virtual attribute takes precedence. |
public
string
|
$sortVar | 'sort' |
#
the name of the GET parameter that specifies which attributes to be sorted in which direction. Defaults to 'sort'. |
public
string
|
$descTag | 'desc' |
#
the tag appeared in the GET parameter that indicates the attribute should be sorted in descending order. Defaults to 'desc'. |
public
mixed
|
$defaultOrder |
|
#
the default order that should be applied to the query criteria when the current request does not specify any sort. For example, 'name, create_time DESC' or 'UPPER(name)'. Starting from version 1.1.3, you can also specify the default order using an
array. The array keys could be attribute names or virtual attribute names as
declared in 'defaultOrder'=>array( 'price'=>CSort::SORT_DESC, )
Please note when using array to specify the default order, the corresponding attributes will be put into directions and thus affect how the sort links are rendered (e.g. an arrow may be displayed next to the currently active sort link). |
public
string
|
$route | '' |
#
the route (controller ID and action ID) for generating the sorted contents. Defaults to empty string, meaning using the currently requested route. |
public
array
|
$separators | array('-','.') |
#
separators used in the generated URL. This must be an array consisting of two elements. The first element specifies the character separating different attributes, while the second element specifies the character separating attribute name and the corresponding sort direction. Defaults to array('-','.'). |
public
array
|
$params |
|
#
the additional GET parameters (name=>value) that should be used when generating sort URLs. Defaults to null, meaning using the currently available GET parameters. |
public
string
|
$orderBy |
#
The order-by columns represented by this sort object. This can be put in the ORDER BY clause of a SQL statement. |
public
array
|
$directions |
#
Sort directions indexed by attribute names. The sort direction. Can be either CSort::SORT_ASC for ascending order or CSort::SORT_DESC for descending order. |