Quick Start

Version 3 (Kien La, 2010-06-19 01:30 PM)

1 2 Kien La
h2. Quick Start
2 1
3 1
This guide will show you the bare essentials to get up and running with php-activerecord. I will assume you have downloaded the library into your include_path in a directory called php-activerecord.
4 1
5 1
The first steps are to include the library and define our database connection:
6 1
7 3 Kien La
<pre class="code"><code class="php">
8 1
require_once 'php-activerecord/ActiveRecord.php';
9 1
10 1
ActiveRecord\Config::initialize(function($cfg)
11 1
{
12 1
    $cfg->set_model_directory('models');
13 1
    $cfg->set_connections(array(
14 1
        'development' => 'mysql://username:[email protected]/database_name'));
15 1
});
16 3 Kien La
</code></pre>
17 1
18 1
Next, lets create a model for a table called users. We'll save this class in the file models/User.php
19 1
20 3 Kien La
<pre class="code"><code class="php">
21 1
class User extends ActiveRecord\Model
22 1
{
23 1
}
24 3 Kien La
</code></pre>
25 1
26 1
That's it! Now you can access the users table thru the User model.
27 1
28 3 Kien La
<pre class="code"><code class="php">
29 1
# create Tito
30 1
$user = User::create(array('name' => 'Tito', 'state' => 'VA'));
31 1
32 1
# read Tito
33 1
$user = User::find_by_name('Tito');
34 1
35 1
# update Tito
36 1
$user->name = 'Tito Jr';
37 1
$user->save();
38 1
39 1
# delete Tito
40 1
$user->delete();
41 3 Kien La
</code></pre>
42 1
43 1
Easy, huh?