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:
assignAssign item to class (without loading it)unassignClears the ID assignmentget_idReturn the current item IDassignedVerify if items is assigned (without a db query).existsVerify if the item exists (with a db query)assign_bySelect item ID from a clausegetSelect assigned item dataget_bySelect item data from clausegetsSelect (all) items from clauseget_tableReturn table nameget_primary_keyReturn primary key table fieldset_primary_keySet the primary key table fieldset_aliasSet table aliasget_aliasGet table aliasdeleteDelete current assigned itemdelete_byDelete item(s) from clauseupdateUpdate assigned itemupdate_byUpdate item(s) from clauseinsertInsert item in DBset_messageSet internal messageget_messageReturn internal messagecountCount all results from the table adding eventually a where clauseincreaseIncrease field value for assigned itemincrease_byIncrease field value form a clauseuniqueCheck if a $value is unique in a $field; if an item is assigned, exclude itrandom_uniqueGenerate and return a random and unique stringset_relationCreate a join from this to other modelset_paginationAdd SQL_CALC_FOUND_ROWS to query (to perform pagination)found_rowsReturn 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' => 'user@email.com' )); $data = $this->yourmodel->get(); -
How to update item;
$this->yourmodel->assign(1); $this->yourmodel->update(array( 'email' => 'newuser@email.com' , 'status' => 'enabled' )); -
Ho to insert new item:
$this->yourmodel->insert(array( 'username' => 'app_user' , 'email' => 'app_user@email.com' , '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', 'your@email.it')) { 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.