Browse Source

Added `yii\data\Sort::setAttributeOrders()`

tags/2.0.10
Klimov Paul 8 years ago
parent
commit
90b9ed87bd
  1. 1
      framework/CHANGELOG.md
  2. 28
      framework/data/Sort.php
  3. 48
      tests/framework/data/SortTest.php

1
framework/CHANGELOG.md

@ -30,6 +30,7 @@ Yii Framework 2 Change Log
- Bug #11541: Fixed default MySQL integer display width for unsigned primary key (h311ion, rob006, cebe)
- Bug #12143: Fixed `yii\db\BaseActiveRecord::updateAttributes()` change `isNewRecord` state for the new model (klimov-paul)
- Enh #9708: Added `yii\console\controllers\AssetController::deleteSource` option allowing deletion of the source asset files after compression (pana1990, klimov-paul)
- Enh #10243: Added `yii\data\Sort::setAttributeOrders()` method allowing manual setup of current sort (klimov-paul)
- Enh #10583: Do not silence session errors in debug mode (samdark)
- Enh #11658: Added argument to `yii\grid\ActionColumn::urlCreator` callback, which holds reference to the column instance (klimov-paul)
- Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)

28
framework/data/Sort.php

@ -67,7 +67,7 @@ use yii\web\Request;
* that can lead to pages with the data sorted by the corresponding attributes.
*
* @property array $attributeOrders Sort directions indexed by attribute names. Sort direction can be either
* `SORT_ASC` for ascending order or `SORT_DESC` for descending order. This property is read-only.
* `SORT_ASC` for ascending order or `SORT_DESC` for descending order.
* @property array $orders The columns (keys) and their corresponding sort directions (values). This can be
* passed to [[\yii\db\Query::orderBy()]] to construct a DB query. This property is read-only.
*
@ -266,6 +266,32 @@ class Sort extends Object
}
/**
* Sets up the currently sort information.
* @param array|null $attributeOrders sort directions indexed by attribute names.
* Sort direction can be either `SORT_ASC` for ascending order or
* `SORT_DESC` for descending order.
* @param boolean $validate whether to validate given attribute orders against [[attributes]] and [[enableMultiSort]].
* If validation is enabled incorrect entries will be removed.
* @since 2.0.10
*/
public function setAttributeOrders($attributeOrders, $validate = true)
{
if ($attributeOrders === null || !$validate) {
$this->_attributeOrders = $attributeOrders;
} else {
$this->_attributeOrders = [];
foreach ($attributeOrders as $attribute => $order) {
if (isset($this->attributes[$attribute])) {
$this->_attributeOrders[$attribute] = $order;
if (!$this->enableMultiSort) {
break;
}
}
}
}
}
/**
* Returns the sort direction of the specified attribute in the current request.
* @param string $attribute the attribute name
* @return boolean|null Sort direction of the attribute. Can be either `SORT_ASC`

48
tests/framework/data/SortTest.php

@ -53,6 +53,9 @@ class SortTest extends TestCase
$this->assertEquals(SORT_ASC, $orders['age']);
}
/**
* @depends testGetOrders
*/
public function testGetAttributeOrders()
{
$sort = new Sort([
@ -80,6 +83,9 @@ class SortTest extends TestCase
$this->assertEquals(SORT_ASC, $orders['age']);
}
/**
* @depends testGetAttributeOrders
*/
public function testGetAttributeOrder()
{
$sort = new Sort([
@ -101,6 +107,45 @@ class SortTest extends TestCase
$this->assertNull($sort->getAttributeOrder('xyz'));
}
/**
* @depends testGetAttributeOrders
*/
public function testSetAttributeOrders()
{
$sort = new Sort([
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
'sort' => 'age,-name'
],
'enableMultiSort' => true,
]);
$orders = [
'age' => SORT_DESC,
'name' => SORT_ASC,
];
$sort->setAttributeOrders($orders);
$this->assertEquals($orders, $sort->getAttributeOrders());
$sort->enableMultiSort = false;
$sort->setAttributeOrders($orders);
$this->assertEquals(['age' => SORT_DESC], $sort->getAttributeOrders());
$sort->setAttributeOrders($orders, false);
$this->assertEquals($orders, $sort->getAttributeOrders());
$orders = ['unexistingAttribute' => SORT_ASC];
$sort->setAttributeOrders($orders);
$this->assertEquals([], $sort->getAttributeOrders());
$sort->setAttributeOrders($orders, false);
$this->assertEquals($orders, $sort->getAttributeOrders());
}
public function testCreateSortParam()
{
$sort = new Sort([
@ -150,6 +195,9 @@ class SortTest extends TestCase
$this->assertEquals('/index.php?r=site%2Findex&sort=name%2Cage', $sort->createUrl('name'));
}
/**
* @depends testCreateUrl
*/
public function testLink()
{
$this->mockApplication();

Loading…
Cancel
Save