Quick Start

Version 1 (Kien La, 2010-06-19 12:10 PM) → Version 2/4 (Kien La, 2010-06-19 12:11 PM)


h2. h1. Quick Start

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.

The first steps are to include the library and define our database connection:

<pre class="php">
require_once 'php-activerecord/ActiveRecord.php';

ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory('models');
$cfg->set_connections(array(
'development' => 'mysql://username:password@localhost/database_name'));
});
</pre>

Next, lets create a model for a table called users. We'll save this class in the file models/User.php

<pre class="php">
class User extends ActiveRecord\Model
{
}
</pre>

That's it! Now you can access the users table thru the User model.

<pre class="php">
# create Tito
$user = User::create(array('name' => 'Tito', 'state' => 'VA'));

# read Tito
$user = User::find_by_name('Tito');

# update Tito
$user->name = 'Tito Jr';
$user->save();

# delete Tito
$user->delete();
</pre>

Easy, huh?