1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 02:04:35 +02:00

Update Pages and PagesLoader classes to take advantage of new Database class features

This commit is contained in:
Ryan Cramer
2020-05-22 13:30:06 -04:00
parent bca53c5cf7
commit 05899763e2
2 changed files with 43 additions and 21 deletions

View File

@@ -440,8 +440,12 @@ class Pages extends Wire {
* *
* #pw-group-retrieval * #pw-group-retrieval
* *
* @param array|string|WireArray $ids Array of page IDs, comma or pipe-separated string of page IDs, or single page ID (string or int) * @param array|string|WireArray $ids Any one of the following:
* @param array $options Options to affect behavior. The 'template' and 'parent' options are recommended when you have this info available. * - Single page ID (string or int)
* - Array of page IDs
* - Comma or pipe-separated string of page IDs
* - Array of associative arrays having id and templates_id: [ [ 'id' => 1, 'templates_id' => 2], [ 'id' => 3, 'templates_id' => 4 ] ]
* @param array $options Options to affect behavior. The 'template' option is recommended when you have this info available.
* - `template` (Template|int|string): Template object, name or ID to use for loaded pages. (default=null) * - `template` (Template|int|string): Template object, name or ID to use for loaded pages. (default=null)
* - `parent` (Page|int|string): Parent Page object, ID, or path to use for loaded pages. (default=null) * - `parent` (Page|int|string): Parent Page object, ID, or path to use for loaded pages. (default=null)
* - `cache` (bool): Place loaded pages in memory cache? (default=true) * - `cache` (bool): Place loaded pages in memory cache? (default=true)

View File

@@ -811,15 +811,16 @@ class PagesLoader extends Wire {
// template was not defined with the function call, so we determine // template was not defined with the function call, so we determine
// which templates are used by each of the pages we have to load // which templates are used by each of the pages we have to load
$sql = "SELECT id, templates_id FROM pages WHERE "; $sql = 'SELECT id, templates_id FROM pages';
if($idCnt == 1) { if($idCnt == 1) {
$sql .= "id=" . (int) reset($ids); $query = $database->prepare("$sql WHERE id=:id");
$query->bindValue(':id', (int) reset($ids), \PDO::PARAM_INT);
} else { } else {
$sql .= "id IN(" . implode(",", $ids) . ")"; $ids = array_map('intval', $ids);
$sql = "$sql WHERE id IN(" . implode(',', $ids) . ")";
$query = $database->prepare($sql);
} }
$query = $database->prepare($sql);
$result = $database->execute($query); $result = $database->execute($query);
if($result) { if($result) {
/** @noinspection PhpAssignmentInConditionInspection */ /** @noinspection PhpAssignmentInConditionInspection */
@@ -858,16 +859,19 @@ class PagesLoader extends Wire {
$query = $this->wire(new DatabaseQuerySelect()); $query = $this->wire(new DatabaseQuerySelect());
$sortfield = $template ? $template->sortfield : ''; $sortfield = $template ? $template->sortfield : '';
$joinSortfield = empty($sortfield) && $options['joinSortfield']; $joinSortfield = empty($sortfield) && $options['joinSortfield'];
// note that "false AS isLoaded" triggers the setIsLoaded() function in Page intentionally
$select = 'false AS isLoaded, pages.templates_id AS templates_id, pages.*, ';
if($joinSortfield) {
$select .= 'pages_sortfields.sortfield, ';
}
if($options['getNumChildren']) {
$select .= "\n(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren";
}
$query->select( $query->select(rtrim($select, ', '));
// note that "false AS isLoaded" triggers the setIsLoaded() function in Page intentionally $query->from('pages');
"false AS isLoaded, pages.templates_id AS templates_id, pages.*, " .
($joinSortfield ? 'pages_sortfields.sortfield, ' : '') .
($options['getNumChildren'] ? '(SELECT COUNT(*) FROM pages AS children WHERE children.parent_id=pages.id) AS numChildren' : '')
);
if($joinSortfield) $query->leftjoin('pages_sortfields ON pages_sortfields.pages_id=pages.id'); if($joinSortfield) $query->leftjoin('pages_sortfields ON pages_sortfields.pages_id=pages.id');
$query->groupby('pages.id');
if($options['autojoin'] && $this->autojoin) { if($options['autojoin'] && $this->autojoin) {
foreach($fields as $field) { foreach($fields as $field) {
@@ -886,13 +890,27 @@ class PagesLoader extends Wire {
$query->leftjoin("$table ON $table.pages_id=pages.id"); // QA $query->leftjoin("$table ON $table.pages_id=pages.id"); // QA
} }
} }
if(count($ids) > 1) {
$ids = array_map('intval', $ids);
$query->where('pages.id IN(' . implode(',', $ids) . ')');
} else {
$id = reset($ids);
$query->where('pages.id=:id');
$query->bindValue(':id', (int) $id, \PDO::PARAM_INT);
}
if(!is_null($parent_id)) $query->where("pages.parent_id=" . (int) $parent_id); if(!is_null($parent_id)) {
if($template) $query->where("pages.templates_id=" . ((int) $template->id)); // QA $query->where('pages.parent_id=:parent_id');
$query->bindValue(':parent_id', (int) $parent_id, \PDO::PARAM_INT);
$query->where("pages.id IN(" . implode(',', $ids) . ") "); // QA }
$query->from("pages");
if($template) {
$query->where('pages.templates_id=:templates_id');
$query->bindValue(':templates_id', (int) $template->id, \PDO::PARAM_INT);
}
$query->groupby('pages.id');
$stmt = $query->prepare(); $stmt = $query->prepare();
$database->execute($stmt); $database->execute($stmt);
@@ -1327,7 +1345,7 @@ class PagesLoader extends Wire {
public function getNativeColumns() { public function getNativeColumns() {
if(empty($this->nativeColumns)) { if(empty($this->nativeColumns)) {
$query = $this->wire('database')->prepare("SELECT * FROM pages WHERE id=:id"); $query = $this->wire('database')->prepare("SELECT * FROM pages WHERE id=:id");
$query->bindValue(':id', $this->wire('config')->rootPageID); $query->bindValue(':id', $this->wire('config')->rootPageID, \PDO::PARAM_INT);
$query->execute(); $query->execute();
$row = $query->fetch(\PDO::FETCH_ASSOC); $row = $query->fetch(\PDO::FETCH_ASSOC);
foreach(array_keys($row) as $colName) { foreach(array_keys($row) as $colName) {