/** * Provide a default sorting strategy for reference columns. * * @param DbField $field * @param Select $select * @param string $direction * @return Select * @throws Select\SelectException */ public function sortDbReference(DbField $field, Select $select, $direction) { $optionPairs = $field->getOptionPairs(); $tableName = $optionPairs->getTableName(); try { $titleColumn = $optionPairs->detectTitleColumn(); } catch (TitleColumnNotDetectedException $e) { $titleColumn = $field->getName(); } if ($titleColumn instanceof Expr) { $orderSpec = "{$titleColumn} {$direction}"; } else { $orderSpec = new Expr("{$select->quoteWithAlias($tableName, $titleColumn)} {$direction}"); } return $select->order($orderSpec); }
/** * Attempt to order the options statement using the options table * metadata. Dewdrop supports two manual sorting columns by * convention: sort_index and sort_order. If either of those * columns is present in your options table, the options will be * sorted by them. Otherwise, we'll sort by the title column. * * @param array $columns The columns portion of the table metadata. * @param Select $stmt * @return \Dewdrop\Db\Select */ protected function orderStmt(array $columns, Select $stmt) { $sortColumn = null; if (array_key_exists('sort_index', $columns)) { $sortColumn = 'sort_index'; } elseif (array_key_exists('sort_order', $columns)) { $sortColumn = 'sort_order'; } if ($sortColumn) { $primaryKey = null; foreach ($columns as $column => $meta) { if ($meta['PRIMARY']) { $primaryKey = $column; break; } } return $stmt->order(array($this->tableName . '.' . $sortColumn, $this->tableName . '.' . $primaryKey)); } elseif ($this->titleColumn instanceof Expr) { return $stmt->order($this->titleColumn); } else { return $stmt->order($this->tableName . '.' . $this->titleColumn); } }