This is my base model that extends CI_Model and is extended from other Model.
####How to install
- Add the file MY_Model.php in /application/core folder.
####How to use
-
Extend your model with:
class Your_Model extends MY_Model { …
-
Define model table name in model:
protected $_table = 'your_table_name'; protected $_table_alias = 'your_table_alias_name';
-
Use it.
####Public function list:
assign
Assign item to class (without loading it)unassign
Clears the ID assignmentget_id
Return the current item IDassigned
Verify if items is assigned (without a db query).exists
Verify if the item exists (with a db query)assign_by
Select item ID from a clauseget
Select assigned item dataget_by
Select item data from clausegets
Select (all) items from clauseget_table
Return table nameget_primary_key
Return primary key table fieldset_primary_key
Set the primary key table fieldset_alias
Set table aliasget_alias
Get table aliasdelete
Delete current assigned itemdelete_by
Delete item(s) from clauseupdate
Update assigned itemupdate_by
Update item(s) from clauseinsert
Insert item in DBset_message
Set internal messageget_message
Return internal messagecount
Count all results from the table adding eventually a where clauseincrease
Increase field value for assigned itemincrease_by
Increase field value form a clauseunique
Check if a $value is unique in a $field; if an item is assigned, exclude itrandom_unique
Generate and return a random and unique stringset_relation
Create a join from this to other modelset_pagination
Add SQL_CALC_FOUND_ROWS to query (to perform pagination)found_rows
Return rows founded after SQL_CALC_FOUND_ROWS (to perform pagination)
###How to work
-
How to select item data:
$this->yourmodel->assign(1); $data = $this->yourmodel->get();
-
How to assign item by email and get data:
$this->yourmodel->assign_by(array( 'email' => '[email protected]' )); $data = $this->yourmodel->get();
-
How to update item;
$this->yourmodel->assign(1); $this->yourmodel->update(array( 'email' => '[email protected]' , 'status' => 'enabled' ));
-
Ho to insert new item:
$this->yourmodel->insert(array( 'username' => 'app_user' , 'email' => '[email protected]' , 'status' => 'disabled' , 'date' => date('Y-m-d H:i:s') ));
-
How to select all items:
$this->yourmodel->gets();
-
How to select all enabled items:
$this->yourmodel->gets(array( 'status' => 'enabled' ));
-
How to delete all disabled items:
$this->yourmodel->delete_by(array( 'status' => 'disabled' ));
-
Hot to assign after insert callback:
In Your_Model add this variable:
public $before_insert = array( 'your_method' ); protected function your_method($boot_data) { // work with $book_data return $book_data; }
-
Hot to count table results:
$n = $this->yourmodel->count();
or
$n = $this->yourmodel->count(array( 'status' => 'enabled' , 'verified' => 1 ));
-
How to increase a table field:
$this->yourmodel->assign($item_id) ->increase('read_counter');
or
$this->yourmodel->increase_by('read_counter', array( 'category' => $category_id ));
-
How to check if a field is unique:
if ( ! $this->yourmodel->unique('email', '[email protected]')) { return FALSE; }
or
$this->yourmodel->assign($user_id); if ( ! $this->yourmodel->unique('slug', 'your-post-title')) { return FALSE; }
-
How to get a random and unique field:
$code = $this->yourmodel->random_unique('code'); $pin = $this->yourmodel->random_unique('pin', 'numeric', 4); $token = $this->yourmodel->random_unique('token', 'sha1');
-
How to set a relation from this model to another:
$list = $this->yourmodel ->set_alias('y') ->set_relation('your_orther_model', 'y.id = o.other_id', 'o', 'right') ->gets();
-
How to perform pagination:
$per_page = 10; $page = $this->input->get('page'); $data = $this->yourmodel ->set_pagination($per_page, $page) ->gets(); $total_rows = $this->yourmodel->found_rows();
-
And so on.