PHPackages                             ilias/maestro - 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. ilias/maestro

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

ilias/maestro
=============

A PHP object-oriented postgres database manager

1.3.1(1y ago)11351MITPHP

Since Aug 6Pushed 5mo ago3 watchersCompare

[ Source](https://github.com/iloElias/choir-maestro)[ Packagist](https://packagist.org/packages/ilias/maestro)[ Docs](https://github.com/iloElias/maestro)[ RSS](/packages/ilias-maestro/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (30)Used By (1)

Maestro Documentation
=====================

[](#maestro-documentation)

[![Maintainer](https://camo.githubusercontent.com/7747e8335e6e4ef5045ec7ec6217c9f98b3a9c6f85ad84655a0b8e2c6c64f5a3/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d40696c6f456c6961732d626c75652e737667)](https://github.com/iloElias)[![Maintainer](https://camo.githubusercontent.com/d613f2a2d7fc9fc8d849acc5eb7a0dedd58e9b715fbdda007c87dd8b49f6e07a/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d406468656e72697175656172616e7465732d626c75652e737667)](https://github.com/dhenriquearantes)[![Package](https://camo.githubusercontent.com/57c967da5464b40936ad5f3e7eda1c58e3eac7f896f91fc330fed84933acf8c8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b6167652d696c6f656c6961732f6d61657374726f2d6f72616e67652e737667)](https://packagist.org/packages/ilias/maestro)[![Source Code](https://camo.githubusercontent.com/347614626e5c57545e7d1e7d8168b502bd7bf232beac9825909cc1e042dad26a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d696c6f656c6961732f6d61657374726f2d626c75652e737667)](https://github.com/iloElias/maestro)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

Maestro is a PHP library designed to facilitate the creation and management of PostgreSQL database schemas and tables. It allows developers to define schemas and tables using PHP classes, providing a clear and structured way to manage database definitions and relationships.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Installation](#installation)
- [Schema and Table Classes](#schema-and-table-classes)
- [Defining Schemas and Tables](#defining-schemas-and-tables)
    - [Schema Class](#schema-class)
    - [Table Class](#table-class)
    - [Unique Columns](#unique-columns)
    - [Default Values](#default-values)
- [DatabaseManager](#databasemanager)
    - [Creating Schemas and Tables](#creating-schemas-and-tables)
    - [Foreign Key Constraints](#foreign-key-constraints)
    - [Executing Queries](#executing-queries)
- [Query Builders](#query-builders)
    - [Select](#select)
    - [Insert](#insert)
    - [Update](#update)
    - [Delete](#delete)
- [Examples](#examples)
    - [Defining a Schema and Tables](#defining-a-schema-and-tables)
    - [Generating SQL Queries](#generating-sql-queries)

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

[](#installation)

To install Maestro, use Composer:

```
composer require ilias/maestro
```

Schema and Table Classes
------------------------

[](#schema-and-table-classes)

Maestro uses abstract classes for schemas and tables. Developers extend these classes to define their own schemas and tables.

Defining Schemas and Tables
---------------------------

[](#defining-schemas-and-tables)

### Schema Class

[](#schema-class)

A schema class extends the `Schema` abstract class. It contains table attributes that are typed with the table classes.

### Table Class

[](#table-class)

A table class extends the `Table` abstract class. It can define columns as class properties, specifying their types and optional default values.

### Unique Columns

[](#unique-columns)

You can specify columns that should be unique by overriding the `tableUniqueColumns` method in your table class. You can also define a column as unique by adding the `@unique` clause to the documentation of the attribute you want to make unique. This format will not work if the `tableUniqueColumns` method is overridden.

### Default Values

[](#default-values)

Columns can have default values. If a default value is a PostgreSQL function, it should be defined as a `PostgresFunction` type to ensure it is not quoted in the final SQL query.

DatabaseManager
---------------

[](#databasemanager)

The `DatabaseManager` class provides methods to create schemas, tables, and manage foreign key constraints.

### Creating Schemas and Tables

[](#creating-schemas-and-tables)

The `createSchema` and `createTable` methods generate SQL queries to create schemas and tables. The `createTablesForSchema` method handles the creation of all tables within a schema and their foreign key constraints.

### Foreign Key Constraints

[](#foreign-key-constraints)

Foreign key constraints are added using `ALTER TABLE` statements after the tables are created.

### Executing Queries

[](#executing-queries)

The `executeQuery` method executes the generated SQL queries using a PDO instance.

Query Builders
--------------

[](#query-builders)

Maestro provides query builder classes for common SQL operations: `Select`, `Insert`, `Update`, and `Delete`.

### Select

[](#select)

The `Select` class allows you to build and execute SELECT queries.

```
use Ilias\Maestro\Database\Select;
use Ilias\Maestro\Database\Connection;

$select = new Select(Connection::get());
$select->from(['u' => 'users'], ['u.id', 'u.name'])
       ->where(['u.active' => true])
       ->order('u.name', 'ASC')
       ->limit(10);

$sql = $select->getSql();
$params = $select->getParameters();
```

### Insert

[](#insert)

The `Insert` class allows you to build and execute INSERT queries.

```
use Ilias\Maestro\Database\Insert;
use Ilias\Maestro\Database\Connection;
use Maestro\Example\User;

$user = new User('John Doe', 'john@example.com', md5('password'), true, new Timestamp('now'));

$insert = new Insert(Connection::get());
$insert->into(User::class)
       ->values($user)
       ->returning(['id']);

$sql = $insert->getSql();
$params = $insert->getParameters();
```

### Update

[](#update)

The `Update` class allows you to build and execute UPDATE queries.

```
use Ilias\Maestro\Database\Update;
use Ilias\Maestro\Database\Connection;

$update = new Update(Connection::get());
$update->table('users')
       ->set('name', 'Jane Doe')
       ->where(['id' => 1]);

$sql = $update->getSql();
$params = $update->getParameters();
```

### Delete

[](#delete)

The `Delete` class allows you to build and execute DELETE queries.

```
use Ilias\Maestro\Database\Delete;
use Ilias\Maestro\Database\Connection;

$delete = new Delete(Connection::get());
$delete->from('users')
       ->where(['id' => 1]);

$sql = $delete->getSql();
$params = $delete->getParameters();
```

### Examples

[](#examples)

#### Defining a Schema and Tables

[](#defining-a-schema-and-tables)

```
