[ XAO ] OPS CONSOLE
SLUG: database-aggregationSECTION: Framework ReferenceSTATUS: stableWORDS: 164READ: 1mENGINE: phc/xsl3

Database Aggregation (DbToXml)

Convert PDO queries, PDOStatement results, and PHP iterables directly into structured DomDoc XML fragments under PHC.

View source XML

Xao\DbToXml replaces Xao's legacy 2003 PEAR DB ResultFetcher with modern PDO / PDOStatement and PHP iterable serialization, producing clean DomDoc XML trees ready to graft into an application document.

01API Methods

Source: src/DbToXml.php (extends XaoRoot)

  • fromQuery(PDO $pdo, string $sql, array $params = [], string $rootTag = 'result', string $rowTag = 'row'): DomDoc

  • fromStatement(PDOStatement $statement, string $rootTag = 'result', string $rowTag = 'row'): DomDoc

  • fromIterable(iterable $rows, string $rootTag = 'result', string $rowTag = 'row'): DomDoc

02Example: Grafting SQLite Rows Into a Page Document
<?php

declare(strict_types=1);

use Xao\DbToXml;
use Xao\DomDoc;

$pdo = new PDO('sqlite:' . __DIR__ . '/data/catalog.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db = new DbToXml();
$itemsDoc = $db->fromQuery(
    $pdo,
    'SELECT slug, title, category, summary FROM items WHERE category = :cat ORDER BY title',
    [':cat' => 'core'],
    'items',
    'item'
);

$page = new DomDoc('catalog');
$page->ndConsumeDoc($itemsDoc);
echo $page->toXml(true);
03Error Handling & Parity

Invalid SQL queries, connection errors, or invalid XML tag names throw Xao\Exceptions\DbToXmlException (or record structured error state on XaoRoot) rather than crashing silently or exposing raw database exceptions to HTTP clients.

04PHC Sandbox Requirements

When running file-backed SQLite databases under phc serve, configure phc.toml:

[sandbox]
allowed_roots = ["public", "src", "stylesheets", "data"]
allow_file_databases = true

Both allow_file_databases = true and listing the database directory in allowed_roots are required by PHC's deny-by-default sandbox.