Extending the Query Builder

The LEAP ORM is capable of extending the query builder for a particular ORM model. This capability allows one to encapsulate common build instructions into one function call and to reduce typing.

To extend a particular ORM model’s builder, create a class that extends DB_ORM_Builder and add the new build instructions like so:

 1 <?php defined('SYSPATH') or die('No direct script access.');
 2 
 3 class Builder_Leap_User extends DB_ORM_Builder {
 4 
 5    public function has_credentials($username, $password) {
 6        $this->builder->where('Username', '=', trim($username));
 7        $this->builder->where('Password', '=', sha1(trim($password)));
 8        return $this;
 9    }
10 
11    public function is_active($status = TRUE) {
12        $this->builder->where('IsActive', '=', (bool) $status);
13        return $this;
14    }
15 
16 }

Now, use DB_ORM to create a query builder for the specified model and call the newly defined build instructions as the following example demonstrates:

1 <?php
2 $results = DB_ORM::select('user')
3    ->has_credentials($username, $password)
4    ->is_active()
5    ->query();