Skip to content
This repository was archived by the owner on Oct 11, 2019. It is now read-only.

Documentation

Circle Creative edited this page Oct 29, 2015 · 1 revision

Installation

The best way to install O2ORM is to use [Composer][9]

composer require o2system/o2orm:'dev-master'

Defining Model

Models in O2ORM (as in other ORMs) represent a single table to work with. To define a model, the ORM model should extends O2System\ORM\Model class.

use O2System\ORM\Model;

class User extends Model
{
    public $table = 'tm_users_accounts';
    public $primary_key = 'id';
}

Querying Model

Get all rows from model

// Method 1:
$users = Users::all();

// Method 2:
$users = Users::rows();

Get all rows from model

/*
 * @params array|null  $conditions
 */
$users = Users::all();

Finding row from model

Find single record base on criteria by specific field.

/*
 * @params string $criteria
 * @params string $field
 */
// Find by primary key field
$user = Users::find(1);

// Find by custom field
$user = Users::find('john_doe', 'username');

Find single record by custom conditions.

/*
 * @params array  $criteria
 * @params string $field
 */
$user = Users::find_by(['username' => 'john_doe', 'password' => 'mysecretpass']);

Finding rows from model

Find many rows within criteria on specific field.

/*
 * @params string $criteria
 * @params string $field
 */
$users = Users::find_many('ACTIVE', 'status');

Find many rows based on certain conditions.

/*
 * @params array  $conditions
 */
$users = Users::find_many_by(['status' => 'ACTIVE']);

Find many rows within criteria on specific field.

/*
 * @params array  $criteria
 * @params string $field
 */
$users = Users::find_in(['ACTIVE','ONLINE'], 'status');

Find many rows not within criteria on specific field.

/*
 * @params array  $criteria
 * @params string $field
 */
$users = Users::find_not_in(['DISABLED','OFFLINE'], 'status');

Relationship Model

Defining One to One Relationship

This the example how to define a one-to-one relationship between a User model with Address. In this case, a User might have one Address.

use O2System\ORM\Model;

class User extends Model
{
     public $table = 'tm_users_accounts';
     public $primary_key = 'id';

     public function address()
     {
         return $this->has_one('Address');
     }
}

class Address extends Model
{
     public $table = 't_users_address';
     public $primary_key = 'id';
}

$user = User::find(1);
echo $user->address->street;

Defining the Inverse Relationship

You can also define the inverse of the relationship. For the example after you get a Address object, you want to know who is the name owner. In the Address model you have to call the belongs_to method.

use O2System\ORM\Model;

class Address extends Model
{
     public $table = 't_users_address';
     public $primary_key = 'id';

     public function owner()
     {
          return $this->belongs_to('User');
     }
}

$address = Address::find(1);
echo $address->owner->username;

Defining One to Many Relationship

An example of one to many relationship is an article can has one or many comments.

use O2System\ORM\Model;

class User extends Model
{
     public $table = 'tm_users_accounts';
     public $primary_key = 'id';

     public function address()
     {
         return $this->has_one('Address');
     }

     public function articles()
     {
         return $this->has_many('Articles');
     }
}

class Articles extends Model
{
     public $table = 't_articles';
     public $primary_key = 'id';
}

$user = User::find(1);
$articles = $user->articles;

echo $user->username;
foreach($articles as $article)
{
     echo $article->title;
}

On the t_articles table you must have field id_user_account or user_account_id.