/** * Get the DatabaseQuerySelect to perform the load operation of items * * @param WireArray $items * @param Selectors|string|null $selectors Selectors or a selector string to find, or NULL to load all. * @return DatabaseQuerySelect * */ protected function getLoadQuery($selectors = null) { $item = $this->makeBlankItem(); $fields = array_keys($item->getTableData()); $table = $this->getTable(); foreach ($fields as $k => $v) { $fields[$k] = "{$table}.{$v}"; } $query = new DatabaseQuerySelect(); $query->select($fields)->from($table); if ($sort = $this->getSort()) { $query->orderby($sort); } $this->getLoadQuerySelectors($selectors, $query); return $query; }
protected function getQuerySortSelector(DatabaseQuerySelect $query, Selector $selector) { $field = is_array($selector->field) ? reset($selector->field) : $selector->field; $values = is_array($selector->value) ? $selector->value : array($selector->value); $fields = $this->fuel('fields'); foreach ($values as $value) { $fc = substr($value, 0, 1); $lc = substr($value, -1); $value = trim($value, "-+"); if (strpos($value, ".")) { list($value, $subValue) = explode(".", $value); } else { $subValue = ''; } if ($value == 'random') { $value = 'RAND()'; } else { if ($value == 'parent') { // sort by parent native field. does not work with non-native parent fields. $tableAlias = "_sort_parent" . ($subValue ? "_{$subValue}" : ''); $query->join("pages AS {$tableAlias} ON {$tableAlias}.id=pages.parent_id"); $value = "{$tableAlias}." . ($subValue ? $subValue : "name"); } else { if ($fields->isNativeName($value)) { if (!strpos($value, ".")) { $value = "pages.{$value}"; } } else { $field = $fields->get($value); if (!$field) { continue; } $tableAlias = "_sort_{$field->name}" . ($subValue ? "_{$subValue}" : ''); $query->leftjoin("{$field->table} AS {$tableAlias} ON {$tableAlias}.pages_id=pages.id"); if ($field->type instanceof FieldtypePage) { // If it's a FieldtypePage, then data isn't worth sorting on because it just contains an ID to the page // so we also join the page and sort on it's name instead of the field's "data" field. $tableAlias2 = "_sort_page_{$field->name}" . ($subValue ? "_{$subValue}" : ''); $query->leftjoin("pages AS {$tableAlias2} ON {$tableAlias}.data={$tableAlias2}.id"); $value = "{$tableAlias2}." . ($subValue ? $subValue : "name"); } else { $value = "{$tableAlias}." . ($subValue ? $subValue : "data"); } } } } if ($fc == '-' || $lc == '-') { $query->orderby("{$value} DESC", true); } else { $query->orderby("{$value}", true); } } }