Skip to content

Commit

Permalink
auto fill sorting colums in ActiveDataProvider if none are configured
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Aug 24, 2013
1 parent ac4b875 commit 713a987
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions framework/yii/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\base\Model;
use yii\db\Query;
use yii\db\ActiveQuery;
use yii\db\Connection;
Expand Down Expand Up @@ -214,4 +216,34 @@ public function refresh()
$this->_totalCount = null;
$this->_keys = null;
}

/**
* Sets the sort definition for this data provider.
* @param array|Sort|boolean $value the sort definition to be used by this data provider.
* This can be one of the following:
*
* - a configuration array for creating the sort definition object. The "class" element defaults
* to 'yii\data\Sort'
* - an instance of [[Sort]] or its subclass
* - false, if sorting needs to be disabled.
*
* @throws InvalidParamException
*/
public function setSort($value)
{
parent::setSort($value);
if (($sort = $this->getSort()) !== false && empty($sort->attributes) &&
$this->query instanceof ActiveQuery) {

/** @var Model $model */
$model = new $this->query->modelClass;
foreach($model->attributes() as $attribute) {
$sort->attributes[$attribute] = array(
'asc' => array($attribute => Sort::ASC),
'desc' => array($attribute => Sort::DESC),
'label' => $model->getAttributeLabel($attribute),
);
}
}
}
}

0 comments on commit 713a987

Please sign in to comment.