2017 © Pedro Peláez
 

library db

Simple PHP database management

image

openclerk/db

Simple PHP database management

  • Friday, January 20, 2017
  • by soundasleep
  • Repository
  • 1 Watchers
  • 1 Stars
  • 206 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

openclerk/db Build Status

A library for MySQL database management in Openclerk, supporting migrations, connection abstraction and automated replication switching., (*1)

Installing

Include openclerk/db as a requirement in your project composer.json, and run composer update to install it into your project:, (*2)

{
  "require": {
    "openclerk/db": "^0.1"
  }
}

Using

Use component-discovery to enable discovery of migrations across all of your dependencies. Update your discovery.json:, (*3)

{
  "components": {
    "migrations": "migrations.json"
  }
}

You can then define your own migrations using migrations.json in each component:, (*4)

{
  "my_migration_1": "\\My\\Migration"
}

Configure your database connection, optionally through a helper function db() (see also openclerk/config project):, (*5)

use \Openclerk\Config;

function db() {
  return new \Db\SoloConnection(
    Config::get("database_name"),
    Config::get("database_username"),
    Config::get("database_password")
  );
}

Load them up and optionally install them at runtime:, (*6)

$logger = new \Monolog\Logger('name');

class AllMigrations extends \Db\Migration {
  function getParents() {
    return array(new Db\BaseMigration()) + DiscoveredComponents\Migrations::getAllInstances();
  }
}

$migrations = new AllMigrations(db());
if ($migrations->hasPending(db())) {
  $migrations->install(db(), $logger);
}

Migrations can be discovered and loaded at runtime with a migrations.json., (*7)

You can also generate migrations at runtime, for example generating a table for each Currency discovered at runtime., (*8)

Replication

You can also define replication connections which are selected based on the type of query, and whether that table has recently been updated in the current $_SESSION:, (*9)

function db() {
  return new \Db\ReplicatedConnection(
    Config::get("database_host_master"),
    Config::get("database_host_slave"),
    Config::get("database_name"),
    Config::get("database_username"),
    Config::get("database_password")
  );
}

Events

A number of events are triggered by the library, and can be captured for metrics:, (*10)

  • db_prepare_start, db_prepare_end, db_prepare_master, db_prepare_slave
  • db_execute_start, db_execute_end
  • db_fetch_start, db_fetch_end
  • db_fetch_all_start, db_fetch_all_end

The Versions

20/01 2017

dev-master

9999999-dev

Simple PHP database management

  Sources   Download

The Requires

 

The Development Requires

14/01 2015

0.1.0

0.1.0.0

Simple PHP database management

  Sources   Download

The Requires

 

The Development Requires