Model - smeeckaert/Orm GitHub Wiki

Model

Behaviour

Models will link your object to the database schema.

Every public property of your model not prefixed by an underscore (_) are considered to be rows of your schema.

Theses properties will be prefixed before any databases operations with $_prefix and an underscore.

For example the property $name

static protected $_prefix = 'user';
public $name;

Will be translated in sql as

user_name

Configuration

There are some protected static variables you have to set before using the model.

  • $_id : The primary id from the database. By default 'id'
  • $_table : The table name
  • $_prefix : The schema's rows prefix
  • $_unique : (optionnal) The list of unique fields
  • $_relations : (optionnal) Relations

Then you just have to declare public property for each of the rows you wish to access via the orm.

Example:

use Orm\Model;
class User extends Model
{
    static protected $_prefix = 'user';
    protected static $_table = 'user';
    protected static $_unique = array('mail', 'login');

    public $id;
    public $mail;
    public $pwd;
    public $login;
}

Public methods

__construct([array])

Create a new object in the model, populated with array content as an associative array.

$user        = new \Api\Model\User(array('login' => 'Snoo');
echo $user->login; // Snoo

find(mixed)

find will return a Model matching your request.

If there is only one entry matching, it will return a Model, otherwise it will return an array of models. Except if the second argument is set to true, then it will always return an array.

find has two distinct behaviour.

First if you give him anything but an array, it will try to find in the schema an item matching $_id to the value.

$user = \Api\Model\User::find(1);
echo $user->login; // Snoo

You can also give him an array containing

array(
'and_where' => '', // See below
'or_where'  => '', // See below
'where'     => '', // The raw content of the query's WHERE
'order'     => array(), // An associative array [field => order]
'limit'     => '', // The limit
)

You can't use and_where, or_where and where parameters at the same time. Only one. Priority is like the array declaration above.

Example with where parameter

$user          = \Api\Model\User::find(array('where' => 'user_login="Snoo"', 'order' => array('login' => 'ASC')));

and_where and or_where parameters will help you to build simple request. The only difference between these two methods are the glue between each element when building the query. and_where will glue with "AND" and or_where with "OR".

The content of these two parameter is an associative array of field => value

$user = \Api\Model\User::find(array('or_where' => array(
        'id'    => 4,
        'login' => 'Snoo'
    )
    )
);

Will result in

SELECT * FROM `user` WHERE `user_id` = "4" OR `user_login` = "Snoo"

If you want to change operators you have to imbricate an other array.

$user = \Api\Model\User::find(array('or_where' => array(
        'id' => 4,
        array(
            array('login' => "Sno%"), ' LIKE ')
    )
    )
);

save

Save or update your item in the database. It will trigger before_save and after_save respectively before and after inserting item in the database

$user        = new \Api\Model\User(array('login' => 'Snoo');
echo $user->id; // Null
echo $user->login; // Snoo
$user->save();
echo $user->id; // 1

delete

Remove the item from the database.

Protected methods

before_save

By default it will try to prevent inserting matching unique keys and throw a Orm\Model\Exception\Unique containing a matching model if it fails.

after_save

Called after the entry has been updated.