-
Notifications
You must be signed in to change notification settings - Fork 1
The ORM library adds additional functionality to ORM classes. To load the ORM library manually you may do this:
load::library('orm');
First of all, if you haven't read Basic ORM
, do so now. As you know, a minimal ORM class may look something like this:
class contacts_orm
{
public $id;
public $first_name;
public $last_name;
}
In order to use the additional functionality that the ORM library provides, you must add some additional code.
class contacts_orm extends orm
{
public $connection = 'default';
public $table = 'contacts';
public $orm = FALSE;
public $id;
public $first_name;
public $last_name;
public function __construct()
{
parent::__construct();
}
}
The $connection
variable is optional and defines what database connection to use for queries. The $table
variable is required and defines what database table to run queries on. The $orm
variable is also optional and if set defines what ORM class to return results as. Note that a value for $orm
variable can be added manually later.
Now you are ready to start using ORM functionality!
###Query Methods There are three basic ways to use ORM classes. The first way is by returning the result of a regular query as an array of ORM classes:
$res = db('table','contacts')->all();
// Later...
$res[0]->delete();
The second method is to manually create an instance of an ORM class and then populate it with column data:
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->delete();
The third method is just like the second method, but you use regular query methods:
$o = load::orm('contacts');
$o-where('first_name','=','Evan');
$o->delete();
###Select
ORM queries are similar to regular queries in many ways. To quickly grab all rows from a table use the all()
method:
// Load ORM instance
$o = load::orm('contacts');
// Run query
$res = $o->all();
When you make queries the results are both returned and stored in the $results
variable:
$o = load::orm('contacts');
$o->all();
print_r($o->results);
You can add data to an ORM object to make simple select queries:
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->last_name = 'Byrne';
$res = $o->select();
More complex queries can be made using regular database methods:
$o = load::orm('contacts');
$o->where('id','=',123456);
$o->limit(1);
$res = $o->select();
Limit, offset, and pagination all work too:
$o = load::orm('contacts');
$o->paginate(1,10,$page);
$res = $o->select();
###Count
Get the total number of rows in a table using total()
:
$o = load::orm('contacts');
$count = $o->total();
You can create complex count queries just like you can with selects.
$o = load::orm('contacts');
$o->where('id','>',5);
$count = $o->count();
$o = load::orm('contacts');
$o->first_name = 'Evan';
$count = $o->count();
###Insert
Simply add column data to your object and run insert()
to insert a new row into a table.
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->last_name = 'Byrne';
$o->insert();
The object is then poplated with any additional information; such as the row ID.
echo $o->id;
###Update You can update rows of data from a previous query easily:
$res = db('table','contacts')->select('first_name','=','Evan');
// Later...
$res[0]->first_name = 'Joe';
$res[0]->update();
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->select();
// Later...
$o->first_name = 'Joe';
$o->update();
Create complex update queries just like you would otherwise:
$o = load::orm('contacts');
$o->where('first_name','=','Evan');
$o->first_name = 'Joe';
$o->update();
###Delete Delete queries work similarily to update queries:
$res = db('table','contacts')->select('first_name','=','Evan');
// Later...
$res[0]->delete();
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->delete();
$o = load::orm('contacts');
$o->first_name = 'Evan';
$o->select();
// Later...
$o->delete();
$o = load::orm('contacts');
$o->where('first_name','=','Evan');
$o->delete();