PHPackages                             jlacroix-dev/pdo-row - 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. jlacroix-dev/pdo-row

ActiveLibrary

jlacroix-dev/pdo-row
====================

Generate typed class for PDO fetchObject

011↓55.6%PHPCI passing

Since Aug 14Pushed 1w agoCompare

[ Source](https://github.com/jlacroix-dev/pdo-row)[ Packagist](https://packagist.org/packages/jlacroix-dev/pdo-row)[ RSS](/packages/jlacroix-dev-pdo-row/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependenciesVersions (3)Used By (0)

PDO Row
=======

[](#pdo-row)

Generate lightweight, typed PHP classes for use with PDO's `fetchObject()`.

PDO already supports hydrating query results into objects. **PDO Row** generates the classes for you based on your database schema.

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

[](#requirements)

- PHP 8.2 or later
- PDO
- MySQL or SQLite

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

[](#installation)

Install PDO Row as a development dependency:

```
composer require --dev jlacroix-dev/pdo-row
```

PDO Row is a code generator, so it normally belongs in `require-dev`.

Quick Start
-----------

[](#quick-start)

### 1. Create the configuration file

[](#1-create-the-configuration-file)

Run:

```
vendor/bin/pdo-row init
```

This creates a `pdo-row.php` file in the current directory.

The generated configuration uses the following environment variables:

```
DB_HOST
DB_DATABASE
DB_USERNAME
DB_PASSWORD
DB_PORT

```

For MySQL, for example:

```
export DB_HOST=127.0.0.1
export DB_DATABASE=my_database
export DB_USERNAME=my_user
export DB_PASSWORD=my_password
export DB_PORT=3306
```

The generated configuration creates a PDO connection and configures the generated classes to be written to:

```
src/Repository/PDO/TableRow

```

with the namespace:

```
App\Repository\PDO\TableRow

```

You can edit `pdo-row.php` to customize these settings.

### 2. Generate the row classes

[](#2-generate-the-row-classes)

Run:

```
vendor/bin/pdo-row generate
```

PDO Row inspects the database schema and generates one PHP class for each selected table.

### 3. Hydrate a query result

[](#3-hydrate-a-query-result)

Given this table:

```
CREATE TABLE users (
    id INT NOT NULL,
    name VARCHAR(255),
    email VARCHAR(255)
);
```

PDO Row can generate:

```
final class UsersTableRow
{
    public int $id;
    public ?string $name;
    public ?string $email;
}
```

You can then hydrate query results directly with PDO:

```
$statement = $pdo->query(
    'SELECT id, name, email FROM users'
);

$user = $statement->fetchObject(UsersTableRow::class);
```

Configuration
-------------

[](#configuration)

A configuration file is a PHP file returning a `Config` instance.

For example:

```
