dev-master
9999999-devSimple PHP database management
The Requires
The Development Requires
0.1.0
0.1.0.0Simple PHP database management
The Requires
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Simple PHP database management
A library for MySQL database management in Openclerk, supporting migrations, connection abstraction and automated replication switching., (*1)
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"
}
}
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)
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")
);
}
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
Simple PHP database management
Simple PHP database management