Configuration Setup
Version 2 (Kien La, 2010-06-19 06:25 PM) → Version 3/4 (Kien La, 2010-08-22 11:49 AM)
h2. Configuration Setup
*(#topic-list) "Default connection":/projects/main/wiki/Configuration__Setup#default-connection
* "Multi-connections":/projects/main/wiki/Configuration__Setup#multi-connections
* "Setting the encoding":/projects/main/wiki/Configuration__Setup#encoding</span>
Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with:
# Setting the model auto_load directory.
# Configuring your database connections.
By setting the model auto_load directory, you are telling PHP where to look for your model classes. This means that you can have an app/folder structure of your choice as long as you have a real directory that holds your model classes. Each class should have it's own php file that is the same name of the class with a .php extension of course.
There are two ways you can initialize your configuration options. The easiest path is wrapping the calls in a closure which is sent through the Config initializer method. This is a neat and clean way to take advantage of PHP's new closure feature.
<pre class="code"><code class="php">
# inclue the ActiveRecord library
require_once 'php-activerecord/ActiveRecord.php';
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections(array('development' =>
'mysql://username:password@localhost/database_name'));
});
</code></pre>
That's it! ActiveRecord takes care of the rest for you. It does not require that you map your table schema to yaml/xml files. It will query the database for this information and cache it so that it does not make multiple calls to the database for a single schema.
If you aren't feeling fancy, you can drop the closure and access the ActiveRecord\Config singleton directly.
<pre class="code"><code class="php">
$cfg = ActiveRecord\Config::instance();
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections(array('development' =>
'mysql://username:password@localhost/database_name'));
</code></pre>
h4(#default-connections). Default connection
The development connection is the default by convention. You can change this by setting a new default connection based off of one of the connections you passed to set_connections.
<pre class="code"><code class="php">
$connections = array(
'development' => 'mysql://username:password@localhost/development',
'production' => 'mysql://username:password@localhost/production',
'test' => 'mysql://username:password@localhost/test'
);
# must issue a "use" statement in your closure if passing variables
ActiveRecord\Config::initialize(function($cfg) use ($connections)
{
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections($connections);
# default connection is now production
$cfg->set_default_connection('production');
});
</code></pre>
h4(#multi-connections). Multi-connections
You can easily configure ActiveRecord to accept multiple database connections. All you have to do is specify the connection in the given model that should be using a different database.
<pre class="code"><code class="php">
$connections = array(
'development' => 'mysql://username:password@localhost/development',
'pgsql' => 'pgsql://username:password@localhost/development',
'sqlite' => 'sqlite://my_database.db',
'oci' => 'oci://username:passsword@localhost/xe'
);
# must issue a "use" statement in your closure if passing variables
ActiveRecord\Config::initialize(function($cfg) use ($connections)
{
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections($connections);
});
</code></pre>
Your models would look like the following.
<pre class="code"><code class="php">
# SomeOciModel.php
class SomeOciModel extends ActiveRecord\Model
{
static $connection = 'oci';
}
# SomeSqliteModel.php
class SomeSqliteModel extends ActiveRecord\Model
{
static $connection = 'sqlite';
}
</code></pre>
You could also have a base 'connection' model so all sub-classes will inherit the db setting.
<pre class="code"><code class="php">
# OciModels.php
abstract class OciModels extends ActiveRecord\Model
{
static $connection = 'oci';
}
# AnotherOciModel.php
class AnotherOciModel extends OciModels
{
# automatically inherits the oci database
}
</code></pre>
h4(#encoding). Setting the encoding
The character encoding can be specified in your connection parameters:
<pre class="code"><code class="php"></span>
$config->set_connections(array(
'mysql' => 'mysql://user:pass@localhost/test?charset=utf8')
);
</code></pre></span>