/** * Sort data * * @param Table $table */ public function sort(Table $table) { $columns = $table->getColumns(); $column = $table->getSortColumn(); $dir = $table->getSortDir(); if (!$column) { return; } $type = $columns[$column]['type']; $cmp = function ($a, $b) use($column, $dir, $type) { $a = $a[$column]; $b = $b[$column]; if ($a === null && $b !== null) { return $dir == Table::DIR_ASC ? -1 : 1; } if ($a !== null && $b === null) { return $dir == Table::DIR_ASC ? 1 : -1; } if ($a === null && $b === null) { return 0; } switch ($type) { case Table::TYPE_BOOLEAN: $a = $a ? 1 : 0; $b = $b ? 1 : 0; case Table::TYPE_INTEGER: case Table::TYPE_FLOAT: case Table::TYPE_DATETIME: if ($a == $b) { return 0; } if ($dir == Table::DIR_ASC) { return $a < $b ? -1 : 1; } return $a < $b ? 1 : -1; case Table::TYPE_STRING: if ($dir == Table::DIR_ASC) { return strcmp($a, $b); } return strcmp($b, $a); default: throw new \Exception("Unknown field type: {$type}"); } }; if (!uasort($this->data, $cmp)) { throw new \Exception('PHP sort failed'); } }
/** * Sort data * * @param Table $table * @return GenericDBAdapter */ public function sort(Table $table) { $column = $table->getSortColumn(); $dir = $table->getSortDir(); if (!$column) { return; } $sqlId = null; foreach ($table->getColumns() as $id => $params) { if ($id == $column) { $sqlId = $params['sql_id']; break; } } if (!$sqlId) { throw new \Exception("No 'sql_id' for column: {$column}"); } $this->sqlOrderBy = $sqlId . " " . $dir; return $this; }
/** * Sort data * * @param Table $table */ public function sort(Table $table) { $column = $table->getSortColumn(); $dir = $table->getSortDir(); if (!$column) { return; } $sqlId = null; foreach ($table->getColumns() as $id => $params) { if ($id == $column) { $sqlId = $params['sql_id']; break; } } if (!$sqlId) { throw new \Exception("No 'sql_id' for column: {$column}"); } $qb = $this->getQueryBuilder(); $qb->addOrderBy($sqlId, $dir); }
/** * Sort data * * @param Table $table */ public function sort(Table $table) { $column = $table->getSortColumn(); $dir = $table->getSortDir(); if (!$column) { return; } $field = null; foreach ($table->getColumns() as $id => $params) { if ($id == $column) { $field = $params['field_name']; break; } } if (!$field) { throw new \Exception("No 'field_name' for column: {$column}"); } $qb = $this->getQueryBuilder(); $qb->sort($field, $dir); }