-
Notifications
You must be signed in to change notification settings - Fork 0
Connecting To A Database
Modus uses Aura.Sql and Aura.SqlQuery as the basis for database connections. Aura.Sql provides an extension of PDO with additional helpful functions; it supports all the drivers available for PDO. Aura.SqlQuery is a query-building library akin to an ORM (but without the overhead). It supports MySQL, Postgres, SQLite and Microsoft SQL Server. Refer to the documentation for Aura.Sql and Aura.SqlQuery for more details of their operation; this documentation refers to configuring and using them within the Modus context.
The default config.php file contains a sample configuration that you can customize your overriding configuration files. The default configuration looks something like this:
/*
* --------------------------------------------------
* Database Configuration
* --------------------------------------------------
*/
'database' => [
'type' => 'mysql',
'dbname' => '',
'default' => [
"user" => "",
"pass" => "",
"host" => "127.0.0.1",
],
'write' => [
'master' => [
"user" => "",
"pass" => "",
"host" => "127.0.0.1",
],
],
'read' => [
'slave' => [
"user" => "",
"pass" => "",
"host" => "127.0.0.1",
],
]
],The database configuration file contains three distinct groups: the default configuration, and a set of read and write configurations.
The default configuration takes exactly one default database; you can have an unlimited number of read and write databases. For example, you could have slave1, slave2 and slave3 for reading, and master1 and master2 for writing.
The actual database configuration takes place in the AppConfig\Database class, where Aura\Sql\ConnectionLocator is configured and made available for use.
Modus uses the Aura.Sql connection locator object for connection storage and management. While the Aura.Sql documentation is far more advanced on the subject, basic usage is as follows:
$default = $locator->getDefault();
$read = $locator->getRead();
$readSpecific = $locator->getRead('my_specific_name');
$write = $locator->getWrite();
$writeSpecific = $locator->getWrite('my_specific_key');All of the instances returned will be extended PDO instances.
Modus includes a base storage class, called Modus\Common\Model\Storage\Database. This class defines two setters: a method called setConnectionLocator() that takes Aura\Sql\ConnectionLocator and a setQueryFactory() method that takes Aura\SqlQuery\QueryFactory. You can extend this object to define your own storage classes.