Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • ActionFormModelBase
  • CActiveDataProvider
  • CalendarEventFormModel
  • CallFormModel
  • CArrayDataProvider
  • CAssetManager
  • CBaseController
  • CBaseUrlRule
  • CCacheHttpSession
  • CClientScript
  • CController
  • CCookieCollection
  • CDataProvider
  • CDataProviderIterator
  • CDbHttpSession
  • CExtController
  • CFormModel
  • CHttpCookie
  • CHttpRequest
  • CHttpSession
  • CHttpSessionIterator
  • COutputEvent
  • CPagination
  • CreatePageFormModel
  • CSort
  • CSqlDataProvider
  • CTheme
  • CThemeManager
  • CUploadedFile
  • CUrlManager
  • CUrlRule
  • CWebApplication
  • CWebModule
  • CWidgetFactory
  • EditMobileFormsFormModel
  • EventCommentPublisherFormModel
  • EventFormModel
  • EventPublisherFormModel
  • FileSystemObjectDataProvider
  • MassActionFormModel
  • MobilePagination
  • NoteFormModel
  • NotificationsController
  • TimeFormModel
  • UploadLogoFormModel
  • X2FormModel
  • X2HttpRequest

Interfaces

  • IDataProvider
  • IWidgetFactory
  • Overview
  • Package
  • Class
  • Tree

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
Extended by CSort

Direct known subclasses

SmartSort
Package: system\web
Copyright: 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
Methods summary
public
# __construct( string $modelClass = null )

Constructor.

Constructor.

Parameters

$modelClass
string
$modelClass the class name of data models that need to be sorted. This should be a child class of CActiveRecord.
public
# applyOrder( CDbCriteria $criteria )

Modifies the query criteria by changing its CDbCriteria::$order property. This method will use directions to determine which columns need to be sorted. They will be put in the ORDER BY clause. If the criteria already has non-empty CDbCriteria::$order value, the new value will be appended to it.

Modifies the query criteria by changing its CDbCriteria::$order property. This method will use directions to determine which columns need to be sorted. They will be put in the ORDER BY clause. If the criteria already has non-empty CDbCriteria::$order value, the new value will be appended to it.

Parameters

$criteria
CDbCriteria
$criteria the query criteria
public string
# getOrderBy( CDbCriteria $criteria = null )

Parameters

$criteria
CDbCriteria
$criteria the query criteria

Returns

string
the order-by columns represented by this sort object. This can be put in the ORDER BY clause of a SQL statement.

Since

1.1.0
public string
# link( string $attribute, string $label = null, array $htmlOptions = array() )

Generates a hyperlink that can be clicked to cause sorting.

Generates a hyperlink that can be clicked to cause sorting.

Parameters

$attribute
string
$attribute the attribute name. This must be the actual attribute name, not alias. If it is an attribute of a related AR object, the name should be prefixed with the relation name (e.g. 'author.name', where 'author' is the relation name).
$label
string
$label the link label. If null, the label will be determined according to the attribute (see CSort::resolveLabel()).
$htmlOptions
array
$htmlOptions additional HTML attributes for the hyperlink tag

Returns

string
the generated hyperlink
public string
# resolveLabel( string $attribute )

Resolves the attribute label for the specified attribute. This will invoke CActiveRecord::getAttributeLabel() to determine what label to use. If the attribute refers to a virtual attribute declared in CSort::$attributes, then the label given in the CSort::$attributes will be returned instead.

Resolves the attribute label for the specified attribute. This will invoke CActiveRecord::getAttributeLabel() to determine what label to use. If the attribute refers to a virtual attribute declared in CSort::$attributes, then the label given in the CSort::$attributes will be returned instead.

Parameters

$attribute
string
$attribute the attribute name.

Returns

string
the attribute label
public array
# getDirections( )

Returns the currently requested sort information.

Returns the currently requested sort information.

Returns

array
sort directions indexed by attribute names. Sort direction can be either CSort::SORT_ASC for ascending order or CSort::SORT_DESC for descending order.
public mixed
# getDirection( string $attribute )

Returns the sort direction of the specified attribute in the current request.

Returns the sort direction of the specified attribute in the current request.

Parameters

$attribute
string
$attribute the attribute name

Returns

mixed
Sort direction of the attribute. Can be either CSort::SORT_ASC for ascending order or CSort::SORT_DESC for descending order. Value is null if the attribute doesn't need to be sorted.
public string
# createUrl( CController $controller, array $directions )

Creates a URL that can lead to generating sorted data.

Creates a URL that can lead to generating sorted data.

Parameters

$controller
CController
$controller the controller that will be used to create the URL.
$directions
array
$directions the 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.

Returns

string
the URL for sorting
public mixed
# resolveAttribute( string $attribute )

Returns the real definition of an attribute given its name.

Returns the real definition of an attribute given its name.

The resolution is based on CSort::$attributes and CActiveRecord::attributeNames().
  • When CSort::$attributes is an empty array, if the name refers to an attribute of CSort::$modelClass, then the name is returned back.
  • When CSort::$attributes is not empty, if the name refers to an attribute declared in CSort::$attributes, then the corresponding virtual attribute definition is returned. Starting from version 1.1.3, if CSort::$attributes contains a star ('*') element, the name will also be used to match against all model attributes.
  • In all other cases, false is returned, meaning the name does not refer to a valid attribute.

Parameters

$attribute
string
$attribute the attribute name that the user requests to sort on

Returns

mixed
the attribute name or the virtual attribute definition. False if the attribute cannot be sorted.
protected CActiveRecord
# getModel( string $className )

Given active record class name returns new model instance.

Given active record class name returns new model instance.

Parameters

$className
string
$className active record class name.

Returns

CActiveRecord
active record model instance.

Since

1.1.14
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.

Creates a hyperlink based on the given label and URL. You may override this method to customize the link generation.

Parameters

$attribute
string
$attribute the name of the attribute that this link is for
$label
string
$label the label of the hyperlink
$url
string
$url the URL
$htmlOptions
array
$htmlOptions additional HTML options

Returns

string
the generated hyperlink
Methods inherited from CComponent
__call(), __get(), __isset(), __set(), __unset(), asa(), attachBehavior(), attachBehaviors(), attachEventHandler(), canGetProperty(), canSetProperty(), detachBehavior(), detachBehaviors(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), getEventHandlers(), hasEvent(), hasEventHandler(), hasProperty(), raiseEvent()
Constants summary
boolean SORT_ASC false
#

Sort ascending

Sort ascending

Since

1.1.10
boolean SORT_DESC true
#

Sort descending

Sort descending

Since

1.1.10
Properties summary
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.

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 CActiveRecord.

the name of the model class whose attributes can be sorted. The model class must be a child class of CActiveRecord.

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 CSort::$modelClass can be sorted. By default, this property is an empty array, which means all attributes in CSort::$modelClass are allowed to be sorted.

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 <span class="php-quote">'user'</span>=><span class="php-quote">'user_id'</span> where 'user' is the name of the virtual attribute while 'user_id' means the virtual attribute is the 'user_id' attribute in the CSort::$modelClass.

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 CSort::$separators.

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.

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 CSort::$modelClass can be sorted. By default, this property is an empty array, which means all attributes in CSort::$modelClass are allowed to be sorted.

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 <span class="php-quote">'user'</span>=><span class="php-quote">'user_id'</span> where 'user' is the name of the virtual attribute while 'user_id' means the virtual attribute is the 'user_id' attribute in the CSort::$modelClass.

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 CSort::$separators.

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'.

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'.

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 CSort::$attributes, and the array values indicate whether the sorting of the corresponding attributes should be in descending order. For example,

'defaultOrder'=>array(
    'price'=>CSort::SORT_DESC,
)

SORT_DESC and SORT_ASC are available since 1.1.10. In earlier Yii versions you should use true and false respectively.

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).

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 CSort::$attributes, and the array values indicate whether the sorting of the corresponding attributes should be in descending order. For example,

'defaultOrder'=>array(
    'price'=>CSort::SORT_DESC,
)

SORT_DESC and SORT_ASC are available since 1.1.10. In earlier Yii versions you should use true and false respectively.

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.

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('-','.').

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.

the additional GET parameters (name=>value) that should be used when generating sort URLs. Defaults to null, meaning using the currently available GET parameters.

Magic properties summary
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.

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.

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.

API documentation generated by ApiGen 2.8.0