Copyright 2013, Markus Hedlund [email protected], Snowfire AB, snowfire.net Licensed under the MIT License. Redistributions of files must retain the above copyright notice.
Example table structure:
order: id, person_id*, message
order_products: order_id*, product_id*
products: id, name
people: id, first_name, last_name
Foreign keys use the foreign singular, and appends _id
. Everything is lowercase and underscore.
None of the following options are required.
protected static $_table = 'addresses';
protected static $_singular = 'address';
protected static $_plural = 'addresses';
protected static $_special_fields = array('create_date' => 'created_on', 'datetime' => array('updated_on'));
protected static $_active_condition = array('state' => 'ACTIVE');
protected static $_many_default_parameters = array('columns' => 'id, property_id, name', 'order_by' => 'order');
protected static $_foreign = array(
array('name' => 'categories', 'type' => 'many_to_many'),
array('name' => 'areas', 'type' => 'many_to_many'),
array('name' => 'addresses', 'type' => 'one_to_one'),
array('name' => 'addresses', 'type' => 'one_to_one', 'singular' => 'billing_address'),
array('name' => 'images', 'type' => 'one_to_one', 'singular' => 'logo_image'),
array('name' => 'images', 'type' => 'one_to_one', 'singular' => 'person_image')
);
Defaults to the class name of the model.
Needs to be set to use foreign keys.
Defaults to the table name.
An array with keys:
create_date
. The column name that contains the created date. Will set this column.datetime
. Columns that should be converted to MySQL datetime. Converts from unix timestamp on inserts, and to unix timestamps on retrieval.
Can be used to make Model::one()
and Model::many()
only find active records, for example.
The variable $_active_condition
will be sent to Database_Query::where()
.
The defaults parameters for Model::many()
. Since Model::one()
uses Model::many()
,
these will also affect that method.
An array defining the foreign keys. Every array item is an array of the following keys:
name
. Foreign resources name / table name.type
.many_to_many
/one_to_many
/one_to_one
.singular
(optional). Will be used for the column name.
Example table structure:
order: id, person_id*
order_products: order_id*, product_id*
products: id, name
Example table structure:
user: id, username
user_notes: id, user_id*, note
Please note that the foreign object will also be deleted, if a one to one relationship is defined.
Example table structure:
order: id, person_id*
people: id, first_name, last_name
These will be triggered before (_pre
) or after (_post
) a CRUD method has executed. Useful to manipulate
the in-data or to do some post processing.
protected function _create_pre(array &$fields, &$foreign_models = array()) {}
protected function _delete_pre(&$rows, &$conditions, &$foreign_models = array()) {}
protected function _delete_post($rows, $conditions, $foreign_models = array()) {}
protected function _edit_pre(&$conditions, array &$fields) {}
Use these methods when you need to write raw queries.
Model::_query_raw()
Model::_query_select([optional select parameters])
Model::_query_update()
Model::_query_insert()
Model::_query_delete()
Model::_query_select()
return a Database_Query
with "select" and "from" set.
Model::_query_update()
return a Database_Query
with "update" set.
Model::_query_insert()
return a Database_Query
with "insert" set.
Model::_query_delete()
return a Database_Query
with "delete" set.
Use Model::_transaction_begin()
and Model::_transaction_end()
for transactions. See Database
.
A model needs to instantiated.
$orders_model = new Orders();
The model provides CRUD functionality:
public function create(array $fields, $foreign_models = array());
public function edit($conditions, array $fields);
public function delete($conditions, $foreign_models = array());
Reading from the DB consists of two methods, Model::one()
and Model::many()
.
Model::one()
fetches the first row, Model::many()
fetches all rows from the result set.
public function one($p = array());
public function many($p = array());
There is also a method to check existence.
public function exists($conditions = array());
Parameter $conditions
is passed to Model::one()
as option conditions
.
If a model have foreigns defined, you must provide instances of these models as well, when using the CRUD methods.
$orders_model->create(array(
'message' => 'Some message'
));
Will create an order and set the message column.
$orders_model->create(array(
'message' => 'Some message',
'person' => array('first_name' => 'Will', 'last_name' = > 'Smith')
), array('people' => new People());
Will create an order, and also a person using the People model. This works for one to one relationships.
For many to many relationships, use:
$orders_model->create(array(
'message' => 'Some message',
'product_ids' => array(4, 9)
), array('products' => new Products());
Edit doesn't affect foreigns.
For one to one relationships, this will delete the foreign object.
For many to many, this will delete the relation.
Model::one()
uses Model::many()
.
columns
. Passed toDatabase_Query::select()
.conditions
. Passed toDatabase_Query::where()
.limit
. Passed toDatabase_Query::limit()
. Limit=1 will return only the first row.order_by
. Passed toDatabase_Query::order_by()
.id_indexed
. Sets the array index of every row to the value of the "id" column.value_column
. Replaces the row with just one column value.ignore_active_condition
. Ignores any$_active_condition
.single_column
. SeeDatabase::one()
/Database::many()
options.foreign_models
. Foreign model instances.
If foreign_models
are provided, and foreigns are setup, the resultset will be populated with foreigns.
Exemple database structure:
order: id, person_id*, message
order_notes: id, order_id*, message
order_products: order_id*, product_id*
products: id, name
people: id, first_name, last_name
Fetching using the Order model, would return something like:
{ id: 1, person: { first_name: 'A', last_name: 'B' } }
Fetching using the Order model, would return something like:
{ id: 1, notes: [{ id: '14', message: 'A' }, { id: '25', name: 'B' }] }
Fetching using the Order model, would return something like:
{ id: 1, products: [{ id: '44', name: 'A' }, { id: '45', name: 'B' }] }