getOrders() public method

Returns the columns and their corresponding sort directions.
public getOrders ( boolean $recalculate = false ) : array
$recalculate boolean whether to recalculate the sort directions
return array the columns (keys) and their corresponding sort directions (values). This can be passed to [[\yii\db\Query::orderBy()]] to construct a DB query.
コード例 #1
0
ファイル: SortTest.php プロジェクト: sciurodont/yii2
 public function testGetOrders()
 {
     $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 = $sort->getOrders();
     $this->assertEquals(3, count($orders));
     $this->assertEquals(SORT_ASC, $orders['age']);
     $this->assertEquals(SORT_DESC, $orders['first_name']);
     $this->assertEquals(SORT_DESC, $orders['last_name']);
     $sort->enableMultiSort = false;
     $orders = $sort->getOrders(true);
     $this->assertEquals(1, count($orders));
     $this->assertEquals(SORT_ASC, $orders['age']);
 }
コード例 #2
0
ファイル: ArrayHelperTest.php プロジェクト: howq/yii2
 public function testMultisortUseSort()
 {
     // single key
     $sort = new Sort(['attributes' => ['name', 'age'], 'defaultOrder' => ['name' => SORT_ASC]]);
     $orders = $sort->getOrders();
     $array = [['name' => 'b', 'age' => 3], ['name' => 'a', 'age' => 1], ['name' => 'c', 'age' => 2]];
     ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
     $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
     $this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
     $this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
     // multiple keys
     $sort = new Sort(['attributes' => ['name', 'age'], 'defaultOrder' => ['name' => SORT_ASC, 'age' => SORT_DESC]]);
     $orders = $sort->getOrders();
     $array = [['name' => 'b', 'age' => 3], ['name' => 'a', 'age' => 2], ['name' => 'a', 'age' => 1]];
     ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
     $this->assertEquals(['name' => 'a', 'age' => 2], $array[0]);
     $this->assertEquals(['name' => 'a', 'age' => 1], $array[1]);
     $this->assertEquals(['name' => 'b', 'age' => 3], $array[2]);
 }
コード例 #3
0
 /**
  * Sorts the data models according to the given sort definition
  *
  * @param array $models the models to be sorted
  * @param \yii\data\Sort  $sort   the sort definition
  *
  * @return array the sorted data models
  */
 protected function sortModels($models, $sort)
 {
     $orders = $sort->getOrders();
     if (!empty($orders)) {
         ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
     }
     return $models;
 }