Configuration Setup

Version 5 (Sergio Figueiredo, 2011-12-26 04:18 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 3 Kien La
* "Setting the encoding":/projects/main/wiki/Configuration__Setup#encoding
6 2 Kien La

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                
113 3 Kien La
h4(#encoding). Setting the encoding
114 3 Kien La

                
115 3 Kien La
The character encoding can be specified in your connection parameters:
116 3 Kien La

                
117 3 Kien La
<pre class="code"><code class="php">
118 3 Kien La
$config->set_connections(array(
119 5 Sergio Figueiredo
  'development' => 'mysql://user:pass@localhost/mydb;charset=utf8')
120 3 Kien La
);
121 1
</code></pre>