Code Monkey home page Code Monkey logo

codeigniter-mymodel's Introduction

CodeIgniter base MyModel

This is my base model that extends CI_Model and is extended from other Model.


####How to install

  1. Add the file MY_Model.php in /application/core folder.

####How to use

  1. Extend your model with:

     class Your_Model extends MY_Model {
     	…
    
  2. Define model table name in model:

     protected $_table = 'your_table_name';
     protected $_table_alias = 'your_table_alias_name';
    
  3. Use it.

####Public function list:

  • assign Assign item to class (without loading it)
  • unassign Clears the ID assignment
  • get_id Return the current item ID
  • assigned 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 clause
  • get Select assigned item data
  • get_by Select item data from clause
  • gets Select (all) items from clause
  • get_table Return table name
  • get_primary_key Return primary key table field
  • set_primary_key Set the primary key table field
  • set_alias Set table alias
  • get_alias Get table alias
  • delete Delete current assigned item
  • delete_by Delete item(s) from clause
  • update Update assigned item
  • update_by Update item(s) from clause
  • insert Insert item in DB
  • set_message Set internal message
  • get_message Return internal message
  • count Count all results from the table adding eventually a where clause
  • increase Increase field value for assigned item
  • increase_by Increase field value form a clause
  • unique Check if a $value is unique in a $field; if an item is assigned, exclude it
  • random_unique Generate and return a random and unique string
  • set_relation Create a join from this to other model
  • set_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

  1. How to select item data:

     $this->yourmodel->assign(1);
     $data = $this->yourmodel->get();
    
  2. How to assign item by email and get data:

     $this->yourmodel->assign_by(array(
     	'email'	=> '[email protected]'
     ));
     $data = $this->yourmodel->get();
    
  3. How to update item;

     $this->yourmodel->assign(1);
     $this->yourmodel->update(array(
     	'email'	=> '[email protected]'
     	, 'status'	=> 'enabled'
     ));
    
  4. 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')
     ));
    
  5. How to select all items:

     $this->yourmodel->gets();
    
  6. How to select all enabled items:

     $this->yourmodel->gets(array(
     	'status'	=> 'enabled'
     ));
    
  7. How to delete all disabled items:

     $this->yourmodel->delete_by(array(
     	'status'	=> 'disabled'
     ));
    
  8. 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;
     }
    
  9. Hot to count table results:

     $n = $this->yourmodel->count();
    

    or

     $n = $this->yourmodel->count(array(
     	'status' => 'enabled'
     	, 'verified' => 1
     ));
    
  10. 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
    ));
    
  11. 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;
    }
    
  12. 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');
    
  13. 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();
    
  14. 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();
    
  15. And so on.

codeigniter-mymodel's People

Stargazers

 avatar  avatar Tommaso Simeone avatar Ahsan Muhammad Yousuf avatar Ammar Jabakji avatar

Watchers

Raj avatar James Cloos avatar Luigi Mozzillo avatar Michele Meta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.