spiral/database

DBAL, schema introspection, migration and pagination

v2.4.1 2019-08-13 12:49 UTC

README

Latest Stable Version Build Status Codecov

Secure, multiple SQL dialects (MySQL, PostgreSQL, SQLite, SQLServer), schema introspection, schema declaration, smart identifier wrappers, database partitions, query builders, nested queries.

Documentation (v1.0.0 - outdated)

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 7.1+
  • PDO Extension with desired database drivers

Installation

To install the component:

$ composer require spiral/database

Example

Given example demonstrates the connection to SQLite database, creation of table schema, data insertion and selection:

<?php
declare(strict_types=1);

require_once "vendor/autoload.php";

use Spiral\Database\Config\DatabaseConfig;
use Spiral\Database\DatabaseManager;
use Spiral\Database\Driver\SQLite\SQLiteDriver;

$dbm = new DatabaseManager(new DatabaseConfig([
    'databases'   => [
        'default' => ['connection' => 'sqlite'],
    ],
    'connections' => [
        'sqlite' => [
            'driver'     => SQLiteDriver::class,
            'connection' => 'sqlite:database.db',
        ],
    ],
]));

$users = $dbm->database('default')->table('users');

// create or update table schema
$schema = $users->getSchema();
$schema->primary('id');
$schema->string('name');
$schema->datetime('created_at');
$schema->datetime('updated_at');
$schema->save();

// insert data
$users->insertOne([
    'name'       => 'test',
    'created_at' => new DateTimeImmutable(),
    'updated_at' => new DateTimeImmutable(),  
]);

// select data
foreach ($users->select()->where(['name' => 'test']) as $u) {
    print_r($u);
}

License:

MIT License (MIT). Please see LICENSE for more information. Maintained by Spiral Scout.