The Database Manager
All database connections are controlled by the database manager. It establishes all connections when started and provides access to the connections via AgaviDatabaseManager::getDatabase().
The database subsystem is enabled by setting use_database" to true in app/config/settings.xml:
... <settings> <setting name="app_name">Bloggie</setting> <setting name="available">true</setting> <setting name="debug">false</setting> <setting name="use_database">true</setting> <setting name="use_logging">false</setting> <setting name="use_security">true</setting> <setting name="use_translation">false</setting> </settings> ...
The connection data is then configured in app/config/databases.xml. We'll assume a MySQL database named "bloggie" on localhost with the user "bloggie" and the password "bloggie".
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0" xmlns="http://agavi.org/agavi/config/parts/databases/1.0">
<ae:configuration environment="development">
<databases default="pdo_mysql_main">
<database name="pdo_mysql_main" class="AgaviPdoDatabase">
<ae:parameter name="dsn">mysql:host=localhost;dbname=bloggie</ae:parameter>
<ae:parameter name="username">bloggie</ae:parameter>
<ae:parameter name="password">bloggie</ae:parameter>
</database>
</databases>
</ae:configuration>
</ae:configurations>
This configures a single database connection named "pdo_mysql_main" that uses the AgaviPdoDatabase adapter. It also sets that connection as default connection.
Accessing a Database connection
A database connection can be accessed via AgaviDatabaseManager::getDatabase($name). If "null" is passed as connection name the default connection is used.
Let's adapt our application to read it's data from that database. You'll see how it pays off that we structured our application nicely - all we need to do is adapt our Posts_PostManager [app/modules/Posts/models/PostManager.class.php] to read it's data from the appropriate tables:
<?php
class Posts_PostManagerModel extends BlogPostsBaseModel
{
public function retrieveById($id)
{
$sql = 'SELECT p.*,
a.screen_name AS author_name,
c.name AS category_name
FROM
posts p
LEFT JOIN
admin_users a ON p.author_id = a.id
LEFT JOIN
categories c ON p.category_id = c.id
WHERE
p.id = ?';
$stmt = $this->getContext()->getDatabaseManager()->getDatabase()->getConnection()->prepare($sql);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (false != $result)
{
return $this->getContext()->getModel('Post', 'Posts', array($result));
}
return null;
}
public function retrieveLatest($limit = 5)
{
$sql = 'SELECT
p.*,
a.screen_name AS author_name,
c.name AS category_name
FROM
posts p
LEFT JOIN
admin_users a ON p.author_id = a.id
LEFT JOIN
categories c ON p.category_id = c.id
ORDER BY
posted DESC
LIMIT ?';
$stmt = $this->getContext()->getDatabaseManager()->getDatabase()->getConnection()->prepare($sql);
$stmt->bindValue(1, $limit, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $post) {
$posts[] = $this->getContext()->getModel('Post', 'Posts', array($post));
}
return $posts;
}
}
?>Well,
that's it. We could certainly do a little cleanup and move the lengthy call to $this->getContext()->getDatabaseManager()->getDatabase()->getConnection()
to an extra method but that won't change the principle. If you fire up your browser, you'll
note no difference.
