Model_Crud Methods

The classes used the examples are assumed to extend the Model_Crud class.

forge($data = array())

The forge method returns a new Model_Crud class instance.

Static Yes
Parameters
Param Type Default Description
$data array
array()
Values for the new instance.
Returns A new instance of the extended class (Model_Crud child).
Example
// Get a new instance
$user = Model_User::forge();

// A new instance with values
$user = Model_User::forge(array(
	'name' => 'John',
	'surname' => 'Doe',
	'email' => 'john@doe.org',
));

find_by_pk($value)

The find_by_pk method finds a record by the primary key. Internally this uses the find_one_by method.

Static Yes
Parameters
Param Type Default Description
$value mixed required Primary key value to look for.
Returns A Model_Crud instance with the loaded result, null when not found.
Example
// Get a record where `id` = 2
$user = Model_User::find_by_pk(2);

if ($user === null)
{
	// not found
}
else
{
	// found
	echo $user->email;
}

find_one_by($column, $value = null, $operator = '=')

The find_one_by method finds one record by the given column and value. Internally this uses the find method.

Static Yes
Parameters
Param Type Default Description
$column string required The column to use.
$value mixed
null
The value to check for.
$operator string
'='
The operator to use
Returns A Model_Crud instance with the loaded result, null when not found.
Example
// Get a record where `email` = "info@example.com"
$user = Model_User::find_one_by('email', 'info@example.com');

if ($user === null)
{
	// not found
}
else
{
	// found
	echo $user->email;
}

find_by($column = null, $value = null, $operator = '=', $limit = null, $offset = 0)

The find_by method finds all records matching the column and value given. You can optionally give an operator, limit, and offset. Internally this uses the find method.

Static Yes
Parameters
Param Type Default Description
$column string required The column to use or an array of where statements.
$value mixed
null
The value to check for.
$operator string
'='
The operator to use.
$limit int
null
The limit.
$offset int
0
The offset.
Returns An array of instances with the loaded result, null when none found.
Example
// SELECT * FROM `users` WHERE `email` LIKE "%@example.com" LIMIT 5 OFFSET 10
$users = Model_User::find_by('email', '%@example.com', 'like', 5, 10);

// Select * FROM `users` WHERE `name` = "John" AND `surname` LIKE "%Surname%" LIMIT 10
$users = \Model_User::find_by(array(
	'name' => 'John',
	array('surname', 'like', '%Surname%'),
), null, null, 10);

if ($users === null)
{
	// not found
}
else
{
	foreach($users as $user)
	{
		// Do something with $user
	}
}

find_by_*($value = null)

This is a magic method. You can specify the column to name to find by after the find_by_ portion of the method name.

Example
// This is equivelent to $user = Model_User::find_by('email', 'foo@example.com');
$user = Model_User::find_by_email('foo@example.com');

find_one_by_*($value = null)

This is a magic method which returns a single record. You can specify the column to name to find by after the find_by_ portion of the method name.

Example
// This is equivelent to $user = Model_User::find_one_by('email', 'foo@example.com');
$user = Model_User::find_one_by_email('foo@example.com');

find_all($limit = null, $offset = 0)

The find_all method finds all record with an optional limit and offset. Internally this uses the find method.

Static Yes
Parameters
Param Type Default Description
$limit int
null
The limit.
$offset int
0
The offset.
Returns An array of instances with the loaded result, null when none found.
Example
// SELECT * FROM `users` LIMIT 10 OFFSET 20
$users = Model_User::find_all(10, 20);

find($config = array(), $key = null)

The find method finds all record with a conditions array to influence the query.

This method also accepts a Closure as the first argument. When a closure is sent, it is called and sent a Database_Query object. You can use this to modify the query before it is executed. The Closure must return the modified Database_Query object.

This method is used by all of the other find methods. You should use it when creating custom finders as well.

Static Yes
Parameters
Param Type Default Description
$config array|Closure
array()
The conditions array or a Closure to modify the query.
$key string
null
Set this to a table column to use that as the result array key.
Returns An array of instances with the loaded result, null when none found.
Example Basic Usage
$users = Model_User::find(array(
	'select' => array('id', 'name', 'email'),
	'where' => array(
		array('name', 'like', '%Name%'),
		'suspended' => 'no',
		array('id', 'in', array(1, 2, 3, 4)),
	),
	'order_by' => array(
		'name' => 'asc',
		'surname' => 'desc',
	),
	'limit' => 10,
	'offset' => 20,
));

Basic Usage with a Key
$users = Model_User::find(array(
	'where' => array(
		'active' => 'yes',
		'banned' => 'no',
	),
	'order_by' => array('id' => 'desc'),
), 'id');

Advanced Usage with Closure
$users = Model_User::find(function ($query)
{
	return $query->join('roles')
	             ->on('roles.id', '=', 'users.role_id')
	             ->where('roles.name', 'admin');
});

pre_find(&$query)

Replace the pre_find method to alter the query before executing.

Static Yes
Parameters
Param Type Default Description
$query \Query_Builder_Select object required The query object.
Returns Void
Example
// Inside your model
protected static function pre_find(&$query)
{
	// alter the query
	$query->where('active', 'yes');
}

post_find($result)

Replace the post_find method to alter the result before returning.

Static Yes
Parameters
Param Type Default Description
$key \Query_Builder_Select object required Null when no records are found, or the result array.
Returns Must return the result (array or null).
Example
// Inside your model
protected static function post_find($result)
{
	if ($result !== null)
	{
		// alter the result array
	}

	// return the result
	return $result;
}

set($values)

The set method sets an array of values to the current model instance.

Static No
Parameters
Param Type Default Description
$values array required An array with values.
Returns The current model instance.
Example
$user = Model_User::forge()->set(array(
	'name' => 'My Name',
	'surname' => 'My Surname',
));

save($validate = true)

The save method inserts or updates a record.

Static No
Parameters
Param Type Default Description
$validate boolean
true
Whether to run the validation.
Returns False when not passing validation, insert result when inserted, update result when updated.
Example
$user = Model_User::forge()->set(array(
	'name' => 'My Name',
	'surname' => 'My Surname',
));

// Insert a new user
$result = $user->save();

$user = Model_User::find_by_pk(2);

$user->name = 'New Name';

// Update a user
$user->save();

// Update without running validation
$user->save(false);

delete()

The delete method deletes a record.

Static No
Parameters None
Returns The delete query result, number of rows affected.
Example
$user = Model_User::find_by_pk(2);
if ($user)
{
	// delete the user;
	$user->delete();
}

is_new($new = null)

The is_new method checks if the record is new or sets whether it is new or not.

Static No
Parameters
Param Type Default Description
$new null|bool
null
Supply a boolean to set whether the record is new.
Returns The current instance.
Example
$user = Model_User::forge(array(
	'id' => 3,
	'name' => 'John',
	'surname' => 'Otherdoe',
));

$user->is_new(false);

// Update
$user->save();

count($column = null, $distinct = true, $where = array(), $group_by = null)

The count method returns a row count.

Static Yes
Parameters
Param Type Default Description
$column string
null
The name of the column to count on. If not given the primary key will be used.
$distinct boolean
true
Whether or not to run a distinct query.
$where array
array()
Array with where clauses to filter the result.
$group_by string
null
Column to group by.
Returns Mixed. Integer with the result of the count query, or false in case the query failed.
Example
// count all records in the table
$nbr_of_records = Model_User::count();

// filter the result count by only counting admin users
$nbr_of_records = Model_User::count('id', true, array('group', '=', 100));

frozen($frozen = null)

The frozen method checks if the record is frozen or sets whether it is frozen or not.

Static No
Parameters
Param Type Default Description
$frozen null|bool
null
Supply a boolean to set whether the record is frozen.
Returns The current instance.
Example
$user = Model_User::find_by_pk(2);
$user->frozen(true);

// Pass the frozen object arround, no-one can change it...

$user->frozen(false);
// Now you can use it again.

validation()

The validation method (creates and) returns the models validation instance.

Static No
Parameters None
Returns A Validation instance.
Example
$user = Model_User::find_by_pk(2);
$validation_object = $user->validation();

validates()

The validates method returns whether or not the instance will pass validation.

Static No
Parameters None
Returns A success boolean.
Example
$model = new Model_User;
$model->set(array(
	'name' => 'John',
	'surname' => 'Doe',
));

if ($model->validates())
{
	// model is valid, skip validation.
	$model->save(false)
}
else
{
	// model is not valid
}

to_array()

The to_array method returns the models public vars.

Static No
Parameters None
Returns array
Example
$user = Model_User::find_by_pk(2);
$user_array = $user->to_array();

pre_save(&$query)

Replace the pre_save method to alter the query before inserting.

Static No
Parameters
Param Type Default Description
$query \Database_Query_Builder_Insert object required The insert query object.
Returns Void
Example
// Inside your model
protected function pre_save(&$query)
{
	// conditionally add a where clause
	if ($something)
	{
		$query->where('field', '=', $something);
	}
}

post_save($result)

Replace the post_save method to alter/act on the insert result.

Static No
Parameters
Param Type Default Description
$result array required The insert result array.
Returns The insert result array.
Example
// Inside your model
protected function post_save($result)
{
	// Do something with the result
	return $result;
}

pre_update(&$query)

Replace the pre_update method to alter the query before updating.

Static Yes
Parameters
Param Type Default Description
$query \Database_Query_Builder_Update object required The update query object.
Returns Void
Example
// Inside your model
protected function pre_update(&$query)
{
	// Change the update query.
	$query->set(array('field' => 'value'));
}

post_update($result)

Replace the post_update method to alter the update query result.

Static No
Parameters
Param Type Default Description
$result int required The number of rows affected.
Returns The update query object.
Example
// Inside your model
protected function post_update($result)
{
	// Do something with the result
	return $result;
}

pre_delete(&$query)

Replace the pre_delete method to alter the query before deleting.

Static No
Parameters
Param Type Default Description
$query \Database_Query_Builder_Delete object required The delete query object.
Returns Void
Example
// Inside your model
protected function pre_delete(&$query)
{
	// Do something with the delete query
}

post_delete($result)

Replace the post_delete method to alter the delete result.

Static No
Parameters
Param Type Default Description
$result int required The number of affected rows.
Returns The number of affected rows.
Example
// Inside your model
protected function post_delete($result)
{
	// Do something with the result
	return $result;
}

pre_validate($data)

Replace the pre_validate method to alter the insert/update data before validation.

Static No
Parameters
Param Type Default Description
$data array required The model's record data.
Returns The model's record data.
Example
// Inside your model
protected function pre_validate($data)
{
	// Do something with the data
	return $data;
}

post_validate($data)

Replace the post_validate method to alter the insert/update data after validation.

Static No
Parameters
Param Type Default Description
$data array required The model's record data.
Returns The model's record data.
Example
// Inside your model
protected function post_validate($data)
{
	// Do something with the data
	return $data;
}

prep_values($data)

Replace the prep_values method to alter the insert/update data before validation or query execution.

Static No
Parameters
Param Type Default Description
$data array required The model's record data.
Returns The model's record data.
Example
// Inside your model
protected function prep_values($data)
{
	// Do something with the data
	return $data;
}