The Db plugin provide an integration of the Db component from Atomik Lib. You can find the full documentation in the Atomik Lib manual.
The plugin can automatically create a default instance (which will be named default). To do so, you must at least define the “dsn” config key (in the plugin configuration).
The username and password key can also be used.
Example 8.1. Automatically connecting to a database
Atomik::set('plugins/Db', array(
'dsn' => 'mysql:host=localhost;dbname=mydb',
'username' => 'root',
'password' => ''
));
The “table_prefix” key can be used to define a default table prefix.
You can use the “db” selector to quickly query the database. It can either a table name or a full sql query. The second argument can be an array of where conditions if a table name is used or an array of parameters in the second case.
Example 8.2. Using the db selector
$users = A('db:users');
$activeUsers = A('db:users', array('active' => 1));
$users = A('db:select * from users');
$activeUsers = A('db:select * from users where active = ?', array(1));
The Db plugin offers facilities to manage sql scripts. If you save your scripts in the
app/sql folder, they can be executed with a single command.
This command is “db-create”. You can also use “db-create-sql” to preview the sql that will be executed.
Example 8.3. Using the command line with the Dbb plugin
php index.php db-create-sql php index.php db-create
Plugins can also provide their own scripts in their own sql folder.
If you want to only execute the sql from certain plugins, you can specify which one, separated by a space, in the sql-create command. To only execute scripts from your application, use App as filter.