PHPackages                             phpro/dbal-tools - 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. phpro/dbal-tools

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

phpro/dbal-tools
================

Tools for working with DBAL in Symfony applications

2.12.0(1mo ago)113123MITPHPPHP ~8.4.0 || ~8.5.0CI passing

Since May 20Pushed 1mo ago5 watchersCompare

[ Source](https://github.com/phpro/dbal-tools)[ Packagist](https://packagist.org/packages/phpro/dbal-tools)[ RSS](/packages/phpro-dbal-tools/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (51)Versions (21)Used By (0)

[![Installs](https://camo.githubusercontent.com/63be7ddadb3ae5b61f5141c3ab2096b89b95f51400148e5076bda2eb7d71b3a1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706870726f2f6462616c2d746f6f6c732e737667)](https://packagist.org/packages/phpro/dbal-tools/stats)[![Packagist](https://camo.githubusercontent.com/a498676ffa02a80fa84bfca7388796cacc5c4cacaaf4310e8ce58995ae61f781/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706870726f2f6462616c2d746f6f6c732e737667)](https://packagist.org/packages/phpro/dbal-tools)

DBAL Tools
==========

[](#dbal-tools)

This package provides a set of tools to work with the Doctrine DBAL.

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

[](#installation)

```
composer require phpro/dbal-tools
```

The package can be used standalone or with Symfony. If you are not using `symfony/flex`, you'll have to manually add the bundle to your bundles file:

```
// config/bundles.php

return [
    // ...
    Phpro\DbalTools\DbalToolsBundle::class => ['all' => true],
];
```

Schema
------

[](#schema)

This package contains a set of schema tools to configure your database schema from within PHP. You can configure following schema types:

### Configuring a table

[](#configuring-a-table)

A table consists out of 2 parts:

- The class that declared the table
- An enum that declares the available table columns:

```
use Phpro\DbalTools\Column\TableColumnsInterface;
use Phpro\DbalTools\Column\TableColumnsTrait;

enum UsersTableColumns: string implements TableColumnsInterface
{
    case Id = 'user_id';
    case Username = 'username';

    use TableColumnsTrait;

    public function linkedTableClass(): string
    {
        return UsersTable::class;
    }
}
```

```
use Doctrine\DBAL\Schema\Table as DoctrineTable;
use Doctrine\DBAL\Types\Types;
use Phpro\DbalTools\Column\Columns;
use Phpro\DbalTools\Schema\Table;

final class UsersTable extends Table
{
    public static function name(): string
    {
        return 'users';
    }

    public static function columns(): Columns
    {
        return Columns::for(UsersTableColumns::class);
    }

    public static function createTable(): DoctrineTable
    {
        return DoctrineTable::editor()
            ->setUnquotedName(self::name())
            ->addColumn(
                Column::editor()
                    ->setUnquotedName(UsersTableColumns::Id->value)
                    ->setTypeName(Types::GUID)
                    ->setNotnull(true)
                    ->create()
            )
            ->addPrimaryKeyConstraint(
                PrimaryKeyConstraint::editor()
                    ->setUnquotedColumnNames(UsersTableColumns::Id->value)
                    ->create()
            )
            ->addColumn(
                Column::editor()
                    ->setUnquotedName(UsersTableColumns::Username->value)
                    ->setTypeName(Types::STRING)
                    ->setLength(255)
                    ->setNotnull(true)
                    ->create()
            )
            ->create();
    }
}
```

You can register this table configuration in your Symfony service configuration:

```
services:
    App\Dbal\Schema\UsersTable:
        tags:
            - 'phpro.dbal_tools.schema.table'
```

This way, it is available inside the `Doctrine\DBAL\Schema\Schema` service and can be used to create the table through migrations.

If you want to make sure that all columns are being used inside the table, you can add a PHPUnit `TableColumnEnumTestCase` for the implementation:

```
