yii\data\Sort::getOrders PHP Method

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.
    public function getOrders($recalculate = false)
    {
        $attributeOrders = $this->getAttributeOrders($recalculate);
        $orders = [];
        foreach ($attributeOrders as $attribute => $direction) {
            $definition = $this->attributes[$attribute];
            $columns = $definition[$direction === SORT_ASC ? 'asc' : 'desc'];
            foreach ($columns as $name => $dir) {
                $orders[$name] = $dir;
            }
        }
        return $orders;
    }

Usage Example

Beispiel #1
0
 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']);
 }
All Usage Examples Of yii\data\Sort::getOrders