Configuration Setup

Version 2 (Kien La, 2010-06-19 06:25 PM)

1 1
h2. Configuration  Setup
2 1

                
3 2 Kien La
*(#topic-list) "Default connection":/projects/main/wiki/Configuration__Setup#default-connection
4 2 Kien La
* "Multi-connections":/projects/main/wiki/Configuration__Setup#multi-connections
5 2 Kien La

                
6 1
Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with:
7 1

                
8 1
# Setting the model auto_load directory.
9 1
# Configuring your database connections.
10 1

                
11 1
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.
12 1

                
13 1
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.
14 1

                
15 1
<pre class="code"><code class="php">
16 1
# inclue the ActiveRecord library
17 1
require_once 'php-activerecord/ActiveRecord.php';
18 1
 
19 1
ActiveRecord\Config::initialize(function($cfg)
20 1
{
21 1
  $cfg->set_model_directory('/path/to/your/model_directory');
22 1
  $cfg->set_connections(array('development' =>
23 1
    'mysql://username:password@localhost/database_name'));
24 1
});
25 1
</code></pre>
26 1

                
27 1
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.
28 1

                
29 1
If you aren't feeling fancy, you can drop the closure and access the ActiveRecord\Config singleton directly.
30 1

                
31 1
<pre class="code"><code class="php">
32 1
$cfg = ActiveRecord\Config::instance();
33 1
$cfg->set_model_directory('/path/to/your/model_directory');
34 1
$cfg->set_connections(array('development' =>
35 1
  'mysql://username:password@localhost/database_name'));
36 1
</code></pre>
37 1

                
38 2 Kien La
h4(#default-connections). Default connection
39 1

                
40 1
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.
41 1

                
42 1
<pre class="code"><code class="php">
43 1
$connections = array(
44 1
  'development' => 'mysql://username:password@localhost/development',
45 1
  'production' => 'mysql://username:password@localhost/production',
46 1
  'test' => 'mysql://username:password@localhost/test'
47 1
);
48 1
 
49 1
# must issue a "use" statement in your closure if passing variables
50 1
ActiveRecord\Config::initialize(function($cfg) use ($connections)
51 1
{
52 1
  $cfg->set_model_directory('/path/to/your/model_directory');
53 1
  $cfg->set_connections($connections);
54 1
 
55 1
  # default connection is now production
56 1
  $cfg->set_default_connection('production');
57 1
});
58 1
</code></pre>
59 1

                
60 2 Kien La
h4(#multi-connections). Multi-connections
61 1

                
62 1
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.
63 1

                
64 1
<pre class="code"><code class="php">
65 1
$connections = array(
66 1
  'development' => 'mysql://username:password@localhost/development',
67 1
  'pgsql' => 'pgsql://username:password@localhost/development',
68 1
  'sqlite' => 'sqlite://my_database.db',
69 1
  'oci' => 'oci://username:passsword@localhost/xe'
70 1
);
71 1
 
72 1
# must issue a "use" statement in your closure if passing variables
73 1
ActiveRecord\Config::initialize(function($cfg) use ($connections)
74 1
{
75 1
  $cfg->set_model_directory('/path/to/your/model_directory');
76 1
  $cfg->set_connections($connections);
77 1
});
78 1
</code></pre>
79 1

                
80 1
Your models would look like the following.
81 1

                
82 1
<pre class="code"><code class="php">
83 1
# SomeOciModel.php
84 1
class SomeOciModel extends ActiveRecord\Model
85 1
{
86 1
  static $connection = 'oci';
87 1
}
88 1
 
89 1
# SomeSqliteModel.php
90 1
class SomeSqliteModel extends ActiveRecord\Model
91 1
{
92 1
  static $connection = 'sqlite';
93 1
}
94 1
</code></pre>
95 1

                
96 1
You could also have a base 'connection' model so all sub-classes will inherit the db setting.
97 1

                
98 1
<pre class="code"><code class="php">
99 1
# OciModels.php
100 1
abstract class OciModels extends ActiveRecord\Model
101 1
{
102 1
  static $connection = 'oci';
103 1
}
104 1
 
105 1
# AnotherOciModel.php
106 1
class AnotherOciModel extends OciModels
107 1
{
108 1
   # automatically inherits the oci database
109 1
}
110 1
</code></pre>