From 713a987a9d803ea9e676026321f7428bdd904dbd Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 25 Aug 2013 00:00:14 +0200 Subject: [PATCH] auto fill sorting colums in ActiveDataProvider if none are configured --- framework/yii/data/ActiveDataProvider.php | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/framework/yii/data/ActiveDataProvider.php b/framework/yii/data/ActiveDataProvider.php index 6dfc9ed..539d00a 100644 --- a/framework/yii/data/ActiveDataProvider.php +++ b/framework/yii/data/ActiveDataProvider.php @@ -9,6 +9,8 @@ namespace yii\data; 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; @@ -214,4 +216,34 @@ class ActiveDataProvider extends DataProvider $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), + ); + } + } + } }