Skip to content
Stefano Azzolini edited this page Jul 15, 2016 · 3 revisions

The Model class allow you to map database table to an object entity.

Create model


class User extends Model {
  // Define the target database table and primary key,
  // 'id' is the default.
  const _PRIMARY_KEY_ = "users.id"

  public $id,
         $email,
         $name;
}

Retrieve all records


$users = User::all();

The all method accept pagination arguments

// Return page 2 with limit 20
$users = User::all(2, 20);

Perform where clause


The where method specifies the WHERE fragment of a SQL query.

// Return all users with gmail's email
$users = User::where("email LIKE '%@gmail.com'");

Create new record


A new model can be created via the ModelName::create factory method.

$mario = User::create([
  "name"  => "Mario",
  "email" => "[email protected]"
]);

The resulting object implements an ActiveRecord pattern for accessing/modifying the model properties.

$mario->name = "Mario De Mario";
$mario->save();
Clone this wiki locally