From 05899763e25db3894cd703ba11e8e05ea964d5ef Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 22 May 2020 13:30:06 -0400 Subject: [PATCH] Update Pages and PagesLoader classes to take advantage of new Database class features --- wire/core/Pages.php | 8 ++++-- wire/core/PagesLoader.php | 56 ++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/wire/core/Pages.php b/wire/core/Pages.php index c65d95f0..7f54789b 100644 --- a/wire/core/Pages.php +++ b/wire/core/Pages.php @@ -440,8 +440,12 @@ class Pages extends Wire { * * #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 $options Options to affect behavior. The 'template' and 'parent' options are recommended when you have this info available. + * @param array|string|WireArray $ids Any one of the following: + * - 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) * - `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) diff --git a/wire/core/PagesLoader.php b/wire/core/PagesLoader.php index 6891fb99..4f58a892 100644 --- a/wire/core/PagesLoader.php +++ b/wire/core/PagesLoader.php @@ -811,15 +811,16 @@ class PagesLoader extends Wire { // template was not defined with the function call, so we determine // 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) { - $sql .= "id=" . (int) reset($ids); + $query = $database->prepare("$sql WHERE id=:id"); + $query->bindValue(':id', (int) reset($ids), \PDO::PARAM_INT); } 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); if($result) { /** @noinspection PhpAssignmentInConditionInspection */ @@ -858,16 +859,19 @@ class PagesLoader extends Wire { $query = $this->wire(new DatabaseQuerySelect()); $sortfield = $template ? $template->sortfield : ''; $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( - // note that "false AS isLoaded" triggers the setIsLoaded() function in Page intentionally - "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' : '') - ); - + $query->select(rtrim($select, ', ')); + $query->from('pages'); if($joinSortfield) $query->leftjoin('pages_sortfields ON pages_sortfields.pages_id=pages.id'); - $query->groupby('pages.id'); if($options['autojoin'] && $this->autojoin) { foreach($fields as $field) { @@ -886,13 +890,27 @@ class PagesLoader extends Wire { $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($template) $query->where("pages.templates_id=" . ((int) $template->id)); // QA - - $query->where("pages.id IN(" . implode(',', $ids) . ") "); // QA - $query->from("pages"); + if(!is_null($parent_id)) { + $query->where('pages.parent_id=:parent_id'); + $query->bindValue(':parent_id', (int) $parent_id, \PDO::PARAM_INT); + } + + 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(); $database->execute($stmt); @@ -1327,7 +1345,7 @@ class PagesLoader extends Wire { public function getNativeColumns() { if(empty($this->nativeColumns)) { $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(); $row = $query->fetch(\PDO::FETCH_ASSOC); foreach(array_keys($row) as $colName) {