1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +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
*
* @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)

View File

@@ -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 */
@@ -859,15 +860,18 @@ class PagesLoader extends Wire {
$sortfield = $template ? $template->sortfield : '';
$joinSortfield = empty($sortfield) && $options['joinSortfield'];
$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' : '')
);
$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(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) {
@@ -887,12 +891,26 @@ class PagesLoader extends Wire {
}
}
if(!is_null($parent_id)) $query->where("pages.parent_id=" . (int) $parent_id);
if($template) $query->where("pages.templates_id=" . ((int) $template->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);
}
$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) {