Commit 713a987a by Carsten Brandt

auto fill sorting colums in ActiveDataProvider if none are configured

parent ac4b8757
...@@ -9,6 +9,8 @@ namespace yii\data; ...@@ -9,6 +9,8 @@ namespace yii\data;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\base\Model;
use yii\db\Query; use yii\db\Query;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
use yii\db\Connection; use yii\db\Connection;
...@@ -214,4 +216,34 @@ class ActiveDataProvider extends DataProvider ...@@ -214,4 +216,34 @@ class ActiveDataProvider extends DataProvider
$this->_totalCount = null; $this->_totalCount = null;
$this->_keys = 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),
);
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment