14. Databases

(how to retrieve a connection, why do it using the framework, propel)

Unlike many other frameworks Agavi doesn't have a database layer of its own but it supports a variety of different database layers and APIs. Currently the following APIs and tools are supported

Your options are however not limited to these because integrating your favourite database tool with Agavi is fairly simple.

Each database implementation is wrapped into a class extending AgaviDatabase.

14.1. Database Connection Configuration

Database connections are configured in app/config/databases.xml. You can have more than one connections.

<?xml version="1.0" encoding="UTF-8"?>
<configurations xmlns="http://agavi.org/agavi/1.0/config">
  <configuration>
    <databases default="postgres">

      <database name="postgres" class="AgaviPostgresqlDatabase">
        <parameters>
          <parameter name="host">localhost</parameter>
          <parameter name="username">username</parameter>
          <parameter name="password">passphrase</parameter>
          <parameter name="database">sample</parameter>
        </parameters>
      </database>

      <database name="propel" class="AgaviPropelDatabase">
        <parameters>
          <parameter name="config">%core.app_dir%/config/project-conf.php</parameter>
        </parameters>
      </database>

    </databases>
  </configuration>
</configurations>

14.2. Using the Connection

Database connections are retrieved using AgaviDatabaseManager.

// retrieve the handle of the default database connection
$conn = $this->context->getDatabaseManager()->getDatabase();
// retrieve the handle of a named database connection
$conn2 = $this->context->getDatabaseManager()->getDatabase('second_database');

// AgaviContext also has a shorcut. You can use it for named connections too.
$conn2 = $this->context->getDatabaseConnection();