PHPackages                             trehinos/thor-pdo-table - 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. trehinos/thor-pdo-table

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

trehinos/thor-pdo-table
=======================

A simple table abstraction for PDO.

v1.1.3(8mo ago)012MITPHPPHP ^8.2

Since Dec 12Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/Trehinos/thor-pdo-table)[ Packagist](https://packagist.org/packages/trehinos/thor-pdo-table)[ RSS](/packages/trehinos-thor-pdo-table/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (7)Used By (0)

Thor PDO Table
==============

[](#thor-pdo-table)

Lightweight utilities to map plain PHP objects (rows) to SQL tables using PHP 8 attributes, with a thin CRUD helper and optional in-memory caching.

This package builds on trehinos/thor-pdo-extension to provide:

- Declarative table and column metadata via PHP attributes
- A CrudHelper to perform typed CRUD operations and hydrate objects
- Drivers to generate DDL (CREATE TABLE/INDEX) for MySQL and SQLite
- A small write-back cache to batch updates

Requirements
------------

[](#requirements)

- PHP 8.1+
- ext-pdo enabled and a PDO driver (mysql or sqlite)
- trehinos/thor-pdo-extension (installed automatically by Composer)

Installation
------------

[](#installation)

Install via Composer:

```
composer require trehinos/thor-pdo-table
```

Core concepts
-------------

[](#core-concepts)

- Row and RowInterface: a Row represents a table row; hydration/serialization is handled by PdoRowTrait.
- Attributes: use PHP 8 attributes (Table, Column, Index) to describe the table schema.
- Primary keys: single or composite primary keys are supported via the Table(primaryKeys: \[...\]) declaration.
- Traits: HasIdTrait adds an auto-increment integer id; HasPublicIdTrait adds a GUID-like public\_id suitable for exposure.
- CrudHelper: thin helper around ArrayCrud to perform create/read/update/delete and to instantiate rows.

Quick start
-----------

[](#quick-start)

### 1) Define a row class with attributes

[](#1-define-a-row-class-with-attributes)

```
use Thor\Database\PdoTable\PdoRow\Row;
use Thor\Database\PdoTable\PdoRow\Attributes\{Table, Column, Index};
use Thor\Database\PdoTable\PdoRow\TableType\{IntegerType, StringType};

#[Table('users', primaryKeys: ['id'], autoColumnName: 'id')]
#[Index(['id'], unique: true)]
abstract class UserRow extends Row
{
    #[Column('id', new IntegerType(), nullable: false)]
    protected ?int $id = null;

    #[Column('username', new StringType(64))]
    protected ?string $username = null;
}
```

Row already provides helper methods through PdoRowTrait to convert to/from arrays.

### 2) Use CrudHelper

[](#2-use-crudhelper)

```
use Thor\Database\PdoExtension\Requester;
use Thor\Database\PdoTable\CrudHelper;

$requester = new Requester($pdo); // from thor-pdo-extension
$crud = new CrudHelper(UserRow::class, $requester);

// Create
$user = new class() extends UserRow {};
$user->fromArray(['username' => 'alice']);
$primary = $crud->createOne($user); // returns primary string; if your row extends AbstractRow with public_id, it returns the public_id

// Read one by primary values (order must match Table primaryKeys)
$loaded = $crud->readOne([1]);

// Update
$loaded->fromArray(['username' => 'alice2']);
$crud->updateOne($loaded);

// Delete
$crud->deleteOne($loaded);
```

### 3) Optional cache

[](#3-optional-cache)

```
use Thor\Database\PdoTable\Cache\Cache;
use Thor\Database\PdoExtension\Criteria;

$cache = new Cache($crud);
$cache->loadAll();                 // warm up cache
$u = $cache->get('1');             // returns associative array or object (depending on CrudHelper)
$cache->set('1', $u);              // mark as pending
$cache->persistAll();              // flush pending changes to DB

// Or load a filtered list
$cache->loadList(Criteria::eq('active', 1));
```

Schema generation
-----------------

[](#schema-generation)

Use SchemaHelper with a driver (MySQL or SQLite) to create/drop tables from your attributes. Debug mode returns SQL instead of executing it.

```
use Thor\Database\PdoExtension\Requester;
use Thor\Database\PdoTable\SchemaHelper;
use Thor\Database\PdoTable\Driver\{MySqlDriver, SqliteDriver};

$requester = new Requester($pdo);
$driver = new MySqlDriver(); // or new SqliteDriver()
$schema = new SchemaHelper($requester, $driver, UserRow::class, isDebug: false);

// Create table and indexes
$schema->createTable();

// Drop table and indexes
$schema->dropTable();

// Debug mode (no execution, returns SQL string)
$schemaDebug = new SchemaHelper($requester, $driver, UserRow::class, isDebug: true);
$sql = $schemaDebug->createTable();
```

Drivers
-------

[](#drivers)

Two drivers help you generate DDL from your attributes:

- MySql: generates CREATE TABLE and related statements for MySQL
- Sqlite: generates CREATE TABLE and CREATE INDEX for SQLite

Both rely on AttributesReader to parse your row class.

Tips and gotchas
----------------

[](#tips-and-gotchas)

- Composite keys: pass ordered values to CrudHelper::readOne(\[...\]) in the same order as declared in Table(primaryKeys: \[...\]).
- Public ID: when using HasPublicIdTrait, remember to call generatePublicId() before insert if you need a deterministic public\_id.
- Requester access: CrudHelper::getRequester() lets you run custom queries alongside CRUD operations.

Development
-----------

[](#development)

- PHP 8.1+
- Run tests with PHPUnit if present in your project
- This package depends on trehinos/thor-pdo-extension

License
-------

[](#license)

MIT Copyright (c) 2021-2025 Sébastien Geldreich

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance58

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~133 days

Recently: every ~141 days

Total

6

Last Release

267d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3908278?v=4)[Trehinos](/maintainers/Trehinos)[@Trehinos](https://github.com/Trehinos)

---

Top Contributors

[![Trehinos](https://avatars.githubusercontent.com/u/3908278?v=4)](https://github.com/Trehinos "Trehinos (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trehinos-thor-pdo-table/health.svg)

```
[![Health](https://phpackages.com/badges/trehinos-thor-pdo-table/health.svg)](https://phpackages.com/packages/trehinos-thor-pdo-table)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
