Configuration Setup
Version 1 (Kien La, 2010-06-19 01:34 PM)
1 | 1 |
h2. Configuration Setup |
|
---|---|---|---|
2 | 1 | ||
3 | 1 |
Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with: |
|
4 | 1 | ||
5 | 1 |
# Setting the model auto_load directory. |
|
6 | 1 |
# Configuring your database connections. |
|
7 | 1 | ||
8 | 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. |
|
9 | 1 | ||
10 | 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. |
|
11 | 1 | ||
12 | 1 |
<pre class="code"><code class="php"> |
|
13 | 1 |
# inclue the ActiveRecord library |
|
14 | 1 |
require_once 'php-activerecord/ActiveRecord.php'; |
|
15 | 1 |
|
|
16 | 1 |
ActiveRecord\Config::initialize(function($cfg) |
|
17 | 1 |
{ |
|
18 | 1 |
$cfg->set_model_directory('/path/to/your/model_directory'); |
|
19 | 1 |
$cfg->set_connections(array('development' => |
|
20 | 1 |
'mysql://username:password@localhost/database_name')); |
|
21 | 1 |
}); |
|
22 | 1 |
</code></pre> |
|
23 | 1 | ||
24 | 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. |
|
25 | 1 | ||
26 | 1 |
If you aren't feeling fancy, you can drop the closure and access the ActiveRecord\Config singleton directly. |
|
27 | 1 | ||
28 | 1 |
<pre class="code"><code class="php"> |
|
29 | 1 |
$cfg = ActiveRecord\Config::instance(); |
|
30 | 1 |
$cfg->set_model_directory('/path/to/your/model_directory'); |
|
31 | 1 |
$cfg->set_connections(array('development' => |
|
32 | 1 |
'mysql://username:password@localhost/database_name')); |
|
33 | 1 |
</code></pre> |
|
34 | 1 | ||
35 | 1 |
h4. Default connection |
|
36 | 1 | ||
37 | 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. |
|
38 | 1 | ||
39 | 1 |
<pre class="code"><code class="php"> |
|
40 | 1 |
$connections = array( |
|
41 | 1 |
'development' => 'mysql://username:password@localhost/development', |
|
42 | 1 |
'production' => 'mysql://username:password@localhost/production', |
|
43 | 1 |
'test' => 'mysql://username:password@localhost/test' |
|
44 | 1 |
); |
|
45 | 1 |
|
|
46 | 1 |
# must issue a "use" statement in your closure if passing variables |
|
47 | 1 |
ActiveRecord\Config::initialize(function($cfg) use ($connections) |
|
48 | 1 |
{ |
|
49 | 1 |
$cfg->set_model_directory('/path/to/your/model_directory'); |
|
50 | 1 |
$cfg->set_connections($connections); |
|
51 | 1 |
|
|
52 | 1 |
# default connection is now production |
|
53 | 1 |
$cfg->set_default_connection('production'); |
|
54 | 1 |
}); |
|
55 | 1 |
</code></pre> |
|
56 | 1 | ||
57 | 1 |
h4. Multi-connections |
|
58 | 1 | ||
59 | 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. |
|
60 | 1 | ||
61 | 1 |
<pre class="code"><code class="php"> |
|
62 | 1 |
$connections = array( |
|
63 | 1 |
'development' => 'mysql://username:password@localhost/development', |
|
64 | 1 |
'pgsql' => 'pgsql://username:password@localhost/development', |
|
65 | 1 |
'sqlite' => 'sqlite://my_database.db', |
|
66 | 1 |
'oci' => 'oci://username:passsword@localhost/xe' |
|
67 | 1 |
); |
|
68 | 1 |
|
|
69 | 1 |
# must issue a "use" statement in your closure if passing variables |
|
70 | 1 |
ActiveRecord\Config::initialize(function($cfg) use ($connections) |
|
71 | 1 |
{ |
|
72 | 1 |
$cfg->set_model_directory('/path/to/your/model_directory'); |
|
73 | 1 |
$cfg->set_connections($connections); |
|
74 | 1 |
}); |
|
75 | 1 |
</code></pre> |
|
76 | 1 | ||
77 | 1 |
Your models would look like the following. |
|
78 | 1 | ||
79 | 1 |
<pre class="code"><code class="php"> |
|
80 | 1 |
# SomeOciModel.php |
|
81 | 1 |
class SomeOciModel extends ActiveRecord\Model |
|
82 | 1 |
{ |
|
83 | 1 |
static $connection = 'oci'; |
|
84 | 1 |
} |
|
85 | 1 |
|
|
86 | 1 |
# SomeSqliteModel.php |
|
87 | 1 |
class SomeSqliteModel extends ActiveRecord\Model |
|
88 | 1 |
{ |
|
89 | 1 |
static $connection = 'sqlite'; |
|
90 | 1 |
} |
|
91 | 1 |
</code></pre> |
|
92 | 1 | ||
93 | 1 |
You could also have a base 'connection' model so all sub-classes will inherit the db setting. |
|
94 | 1 | ||
95 | 1 |
<pre class="code"><code class="php"> |
|
96 | 1 |
# OciModels.php |
|
97 | 1 |
abstract class OciModels extends ActiveRecord\Model |
|
98 | 1 |
{ |
|
99 | 1 |
static $connection = 'oci'; |
|
100 | 1 |
} |
|
101 | 1 |
|
|
102 | 1 |
# AnotherOciModel.php |
|
103 | 1 |
class AnotherOciModel extends OciModels |
|
104 | 1 |
{ |
|
105 | 1 |
# automatically inherits the oci database |
|
106 | 1 |
} |
|
107 | 1 |
</code></pre> |