Skip to content

Instantly share code, notes, and snippets.

@marktopper
Last active October 6, 2016 13:28
Show Gist options
  • Save marktopper/2f783901f19da6597b935e3b432fa41d to your computer and use it in GitHub Desktop.
Save marktopper/2f783901f19da6597b935e3b432fa41d to your computer and use it in GitHub Desktop.
Fistlab PHP Database Repositories

You can use the Container object as repository handler.

This requires the Container component.

use Fist\Database\Database;
use Fist\Container\Container;
use Fist\Repository\ContainerRepository;

$container = new Container();

// The below two lines are not needed, unless you wish to change the default values.
//$container->instance('default.driver', 'mysql');
//$container->instance('default.connection', 'default');

// Method A: You register your connections directly to the container instance (can be done at anytime)
//$container->instance('connections.default', [
//    'driver' => 'mysql',
//    'hostname' => '127.0.0.1',
//    'database' => 'database',
//    'username' => 'root',
//    'password' => '',
//    'prefix' => '',
//]);

// Those below lines are not needed unless you whish to overwrite some connection drivers
//$connection->bind('drivers.mysql', \Fist\Database\Connectors\MysqlConnection::class);
//$connection->bind('drivers.sqlite', \Fist\Database\Connectors\SqliteConnection::class);

$repository = new ContainerRepository($container);

// Method B: You register your connections to the repository (can be done at any time)
//$repository->set('connections.default', [
//    'driver' => 'mysql',
//    'hostname' => '127.0.0.1',
//    'database' => 'database',
//    'username' => 'root',
//    'password' => '',
//    'prefix' => '',
//]);

$database = new Database($repository);

// Method C: You register your connections to the database object (can be done at any time) (recommened)
$database->addConnection('default', [
    'driver' => 'mysql',
    'hostname' => '127.0.0.1',
    'database' => 'database',
    'username' => 'root',
    'password' => '',
    'prefix' => '',
]);

// For changing the default driver
//$database->setDefaultDriver('mysql');

// For changing the default connection
//$database->setDefaultConnection('default');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment