Query_Builder_Where class

The Query_Builder_Where class handles the conditioning and limiting logic for quiry building. Every kind of where statement (including BETWEEN and IN etc...) is handled here. This class is not instantiated (it's abstract) but extended by other classes that share the same functions. There classes are:

Please note: Since Query_Builder_Where is get extended by the classes listed above, the "current instance" returned will be one of those.

Methods

where()

The where method is an alias for and_where.

and_where($column, $op = null, $value = null)

The and_where method appends a WHERE statement.

Static No
Parameters
Param Type Default Description
$column mixed required Column name or array($column, $alias), object, or callback
$op string
null
Logic operator: =, !=, IN, BETWEEN and LIKE. If none is provided this will fallback to '='
$value string
null
Column value
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set the first where statement
$query->where('name', '!=', 'John');

// and append a new condition
$query->and_where('surname', 'Doe');

// nested where through callback
$query->where(function($query){
	$query->where('email', 'jim@jim.com')
		->or_where('email', 'haha@joke.com');
});

// Resulting in:
// SELECT * FROM `users` WHERE `name` != "John" AND `surname` = "Doe"
// AND (`email` = "jim@jim.com" OR `email` = "haha@joke.com")

or_where($column, $op = null, $value = null)

The or_where method appends an OR WHERE statement.

Static No
Parameters
Param Type Default Description
$column string required Column name or array($column, $alias), object or callback
$op string
null
Logic operator: =, !=, IN, BETWEEN. If none is provided this will fallback to '='
$value string
null
Column value
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set the first where statement
$query->where('name', '=', 'John');

// and append a new condition
$query->or_where('surname', 'Doe');

// nested where through callback
$query->or_where(function($query){
	$query->where('email', 'jim@jim.com')
		->and_where('id', 4);
});

// Resulting in:
// SELECT * FROM `users` WHERE `name` != "John" OR `surname` = "Doe"
// OR (`email` = "jim@jim.com" AND `id` = 4)

and_where_open()

The and_where_open method opens an SQL closure and appends it using AND.

Static No
Parameters None
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set a where statement
$query->where('name', 'john');

// Open a closure
$query->and_where_open();

// SELECT * FROM `users` WHERE `name` = "John" AND (

or_where_open()

The or_where_open method opens an SQL closure and appends it using OR.

Static No
Parameters None
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set a where statement
$query->where('name', 'john');

// Open a closure
$query->or_where_open();

// SELECT * FROM `users` WHERE `name` = "John" OR (

and_where_close()

The and_where_close method closes an SQL closure.

Static No
Parameters None
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set a where statement
$query->where('email', 'like', '%@example.com');

// Open a closure
$query->and_where_open();

// Set a where statement
$query->where('name', 'John');
$query->or_where('surname', 'Doe');

$query->and_where_close();
// SELECT * FROM `users`
// WHERE `email` LIKE "%example.com" AND (`name` = "John" OR `surname` = "Doe")

or_where_close()

The or_where_close method closes an SQL closure.

Static No
Parameters None
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set a where statement
$query->where('email', 'like', '%@example.com');

// Open a closure
$query->or_where_open();

// Set a where statement
$query->where('name', 'John');
$query->and_where('surname', 'Doe');

$query->or_where_close();
// SELECT * FROM `users` WHERE `email` LIKE "%example.com" OR (`name` = "John" AND `surname` = "Doe")

order_by($column, $direction = NULL)

The order_by method sets the order for the result/execution.

Static No
Parameters
Param Type Default Description
$column string required Column name
$direction string null Can be set to 'asc' or 'desc'. Ommit for
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set the order by
$query->order_by('name');
$query->order_by('email', 'asc');
$query->order_by('surname', 'desc');

// SELECT * FROM `users` ORDER BY `name`, `email` ASC, `surname DESC

limit($number)

The limit method limits the number of rows selected/affected.

Static No
Parameters
Param Type Default Description
$number int required The number of rows
Returns Returns the current instance.
Example
// prepare a select statement
$query = DB::select('*')->from('users');

// Set the limit
$query->limit(10);

// SELECT * FROM `users` LIMIT 10