PHPackages                             mevdschee/pathpdo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mevdschee/pathpdo

ActiveLibrary[Database &amp; ORM](/categories/database)

mevdschee/pathpdo
=================

JSON interface to a PDO connected SQL database.

11PHPCI failing

Since Mar 16Pushed 2w agoCompare

[ Source](https://github.com/mevdschee/pathpdo)[ Packagist](https://packagist.org/packages/mevdschee/pathpdo)[ RSS](/packages/mevdschee-pathpdo/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependenciesVersions (2)Used By (0)

pathpdo
=======

[](#pathpdo)

A PHP path engine library for PDO. Allows to query the database using PathQL (see: [PathQL.org](https://pathql.org/)).

### Requirements

[](#requirements)

- PHP 8 with JSON
- PDO drivers

Metadata Configuration
----------------------

[](#metadata-configuration)

By default, PathPDO queries the database schema at runtime to determine foreign key relationships for automatic path inference. For better performance, you can cache this metadata in a file.

### Setting a Metadata File

[](#setting-a-metadata-file)

```
use Tqdev\PdoJson\Schema;

// Use a metadata file instead of querying the database
Schema::setMetadataFile('pathpdo.json');

// Or use PHP array format
Schema::setMetadataFile('pathpdo.php');

// Switch back to database-based metadata
Schema::setMetadataFile(null);
```

### Exporting Metadata

[](#exporting-metadata)

To create a metadata file from your current database:

```
$db = PathPdo::create($username, $password, $database);
$schema = new Schema();

// Export as JSON (default)
$schema->exportMetadata($db, 'pathpdo.json');

// Export as PHP array
$schema->exportMetadata($db, 'pathpdo.php', 'php');
```

### Custom Metadata Cache

[](#custom-metadata-cache)

For advanced use cases, you can implement your own metadata caching (e.g., Redis, Memcached, database) using the `setMetaData()` and `getMetaData()`methods:

```
use Tqdev\PdoJson\Schema;

// Example: Caching metadata in Redis
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Check if metadata is cached
if ($redis->exists('pathpdo:metadata')) {
    // Load from cache
    $json = $redis->get('pathpdo:metadata');
    Schema::setMetaData($json);
} else {
    // Generate from database
    $db = PathPdo::create($username, $password, $database);
    $schema = new Schema();
    $json = $schema->getMetaData($db);

    // Store in cache
    $redis->set('pathpdo:metadata', $json, 3600); // Cache for 1 hour
    Schema::setMetaData($json);
}

// Now PathPDO will use the cached metadata
```

The `setMetaData()` method accepts a JSON string in the same format as metadata files, while `getMetaData()` returns the current metadata as JSON (from cache, file, or database).

### Metadata File Format (JSON)

[](#metadata-file-format-json)

```
{
    "foreign_keys": [
        {
            "from_table": "comments",
            "from_column": "post_id",
            "to_table": "posts",
            "to_column": "id"
        },
        {
            "from_table": "posts",
            "from_column": "category_id",
            "to_table": "categories",
            "to_column": "id"
        }
    ]
}
```

### Benefits

[](#benefits)

- **Performance**: Eliminates schema queries on every request
- **Portability**: Works even without direct access to information\_schema
- **Version Control**: Track schema changes in your repository
- **Consistency**: Ensures the same schema interpretation across environments

Using PathQL
------------

[](#using-pathql)

### Basic Query

[](#basic-query)

The `pathQuery()` method executes SQL queries and returns results in a hierarchical structure based on table relationships:

```
$db = PathPdo::create($username, $password, $database);

// Simple query
$results = $db->pathQuery('SELECT `id`,`name` FROM `users`');
// Returns: [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}]

// With parameters (named/ordered)
$results = $db->pathQuery('SELECT * FROM users WHERE id = :id', ['id' => 1]);
$results = $db->pathQuery('SELECT * FROM users WHERE id = ?', [1]);
```

### Automatic Path Inference

[](#automatic-path-inference)

When you do not pass any paths, PathPDO infers the structure from the JOINs in the query and the foreign keys in the schema. Each table alias becomes a JSON key, and the relationship direction decides whether it nests as an array or an object:

```
// One-to-many: comments has a foreign key to posts, so comments nest as an
// array under each post. The aliases (p, c) become the JSON keys.
$db->pathQuery(
    'SELECT p.id, c.id, c.message
     FROM posts p
     LEFT JOIN comments c ON c.post_id = p.id
     WHERE p.id
