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
パラメータ
パラメータ 規定値 説明
$data array
array()
Values for the new instance.
返り値 A new instance of the extended class (Model_Crud child).
// 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
パラメータ
パラメータ 規定値 説明
$value mixed 必須 Primary key value to look for.
返り値 A Model_Crud instance with the loaded result, null when not found.
// 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
パラメータ
パラメータ 規定値 説明
$column string 必須 The column to use.
$value mixed
null
The value to check for.
$operator string
'='
The operator to use
返り値 A Model_Crud instance with the loaded result, null when not found.
// 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
パラメータ
パラメータ 規定値 説明
$column string 必須 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.
返り値 An array of instances with the loaded result, null when none found.
// 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
パラメータ
パラメータ 規定値 説明
$limit int
null
The limit.
$offset int
0
The offset.
返り値 An array of instances with the loaded result, null when none found.
// 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
パラメータ
パラメータ 規定値 説明
$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.
返り値 An array of instances with the loaded result, null when none found.
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
パラメータ
パラメータ 規定値 説明
$query \Query_Builder_Select object 必須 The query object.
返り値 Void
// 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
パラメータ
パラメータ 規定値 説明
$key \Query_Builder_Select object 必須 Null when no records are found, or the result array.
返り値 Must return the result (array or null).
// 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
パラメータ
パラメータ 規定値 説明
$values array 必須 An array with values.
返り値 The current model instance.
$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
パラメータ
パラメータ 規定値 説明
$validate boolean
true
Wether to run the validation.
返り値 False when not passing validation, insert result when inserted, update result when updated.
$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
パラメータ None
返り値 The delete query result, number of rows affected.
$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
パラメータ
パラメータ 規定値 説明
$new null|bool
null
Supply a boolean to set whether the record is new.
返り値 The current instance.
$user = Model_User::forge(array(
	'id' => 3,
	'name' => 'John',
	'surname' => 'Otherdoe',
));

$user->is_new(false);

// Update
$user->save();

is_frozen($frozen = null)

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

Static No
パラメータ
パラメータ 規定値 説明
$frozen null|bool
null
Supply a boolean to set whether the record is frozen.
返り値 The current instance.
$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
パラメータ None
返り値 A Validation instance.
$user = Model_User::find_by_pk(2);
$validation_object = $user->validation();

validates()

The validates method returns wether the instance will pass validation.

Static No
パラメータ None
返り値 A succes boolean.
$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
パラメータ None
返り値 A Validation instance.
$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
パラメータ
パラメータ 規定値 説明
$query \Database_Query_Builder_Insert object 必須 The insert query object.
返り値 Void
// Inside your model
protected function pre_save(&$query)
{
	// change the query
	$query->set(array('field' => 'value'));
}

post_save($result)

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

Static No
パラメータ
パラメータ 規定値 説明
$result array 必須 The insert result array.
返り値 The insert result array.
// 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
パラメータ
パラメータ 規定値 説明
$query \Database_Query_Builder_Update object 必須 The update query object.
返り値 Void
// 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
パラメータ
パラメータ 規定値 説明
$result int 必須 The number of rows affected.
返り値 The update query object.
// 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
パラメータ
パラメータ 規定値 説明
$query \Database_Query_Builder_Delete object 必須 The update query object.
返り値 Void
// 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
パラメータ
パラメータ 規定値 説明
$result int 必須 The number of affected rows.
返り値 The number of affected rows.
// 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
パラメータ
パラメータ 規定値 説明
$data array 必須 The model's record data.
返り値 The model's record data.
// 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
パラメータ
パラメータ 規定値 説明
$data array 必須 The model's record data.
返り値 The model's record data.
// 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
パラメータ
パラメータ 規定値 説明
$data array 必須 The model's record data.
返り値 The model's record data.
// Inside your model
protected function prep_values($data)
{
	// Do something with the data
	return $data;
}