Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have write a general model as for my personal use in CodeIgniter Structure .. I have covered possible all methods which is need to use for data manipulation. </p> <p>copy following code in paste in <strong>general_model.php inside your model directory</strong>...</p> <pre><code>class General_model extends CI_Model { private $_table; private $_fields; public $fields; function __construct() { // Call the Model constructor parent::__construct(); } /** * Set table name * @access public * @param string - sets table_name * @return null * @author Rajnish Savaliya */ function set_table($table_name) { $this-&gt;_table = $table_name; $this-&gt;_fields = $this-&gt;db-&gt;list_fields($this-&gt;_table); foreach($this-&gt;_fields as $field) { $this-&gt;fields[$field] = ""; } } /** * Get record from tables * @access public * @return array() * @author Rajnish Savaliya */ function get_fields_array() { return $this-&gt;_fields; } /** * Get record from table * @access public * @param number - sets limit * @param number - sets offset * @param array - sets order * @author Rajnish Savaliya * @return array() */ function get($select = array(),$conditions = array(),$order=array(),$limit=NULL,$offset=NULL) { $this-&gt;db-&gt;select($select)-&gt;from($this-&gt;_table)-&gt;where($conditions); if($order) $this-&gt;db-&gt;order_by(key($order),$order[key($order)]); if($limit &amp;&amp; $offset) $this-&gt;db-&gt;limit($limit,$offset); elseif($limit) $this-&gt;db-&gt;limit($limit); $query = $this-&gt;db-&gt;get(); return $query-&gt;result_array(); } /** * GET [copy] Get record from table * @access public * @param number - sets limit * @param number - sets offset * @param array - sets order * @author Rajnish Savaliya * @return stdClass() */ function get_stdClass($select = array(),$conditions = array(),$order=array(),$limit=NULL,$offset=NULL) { $this-&gt;db-&gt;select($select)-&gt;from($this-&gt;_table)-&gt;where($conditions); if($order) $this-&gt;db-&gt;order_by(key($order),$order[key($order)]); if($limit &amp;&amp; $offset) $this-&gt;db-&gt;limit($limit,$offset); elseif($limit) $this-&gt;db-&gt;limit($limit); $query = $this-&gt;db-&gt;get(); return $query-&gt;result(); } /** * Advance Get Function * @access public * @param number - select values * @param number - sets limit * @param number - sets offset * @param array - sets order * @param array - sets groupby * @author Rajnish Savaliya * @return array() */ function advance_get($select = array(),$conditions = array(),$order=array(),$groupby='',$limit=NULL,$offset=NULL) { $this-&gt;db-&gt;select($select)-&gt;from($this-&gt;_table)-&gt;where($conditions); if($order) $this-&gt;db-&gt;order_by(key($order),$order[key($order)]); if($groupby != ''){ $this-&gt;db-&gt;group_by($groupby); } $this-&gt;db-&gt;limit($limit,$offset); $query = $this-&gt;db-&gt;get(); return $query-&gt;result_array(); } /** * Get record by id from table * @access public * @param number - sets limit * @param number - sets offset * @param array - sets order * @author Rajnish Savaliya * @return array() */ function get_by_id($id,$order=array("id"=&gt;"ASC"),$limit='1',$offset=NULL) { $this-&gt;db-&gt;where("id",$id); $this-&gt;db-&gt;from($this-&gt;_table)-&gt;order_by(key($order),$order[key($order)])-&gt;limit($limit,$offset); $query = $this-&gt;db-&gt;get(); return $query-&gt;result_array(); } /** * Save record in table * @access public * @param array - * @return insert id * @author Rajnish Savaliya */ function save($data,$password = NULL,$created = NULL) { if(!empty($data)) { //if password field exist then if($password != NULL) { $data[$password] = md5($data[$password]); } if($created != NULL) { $data[$created] = date('Y-m-d H:i:s'); } $data = elements($this-&gt;_fields,$data); $this-&gt;db-&gt;insert($this-&gt;_table, $data); return $this-&gt;db-&gt;insert_id(); } return false; } /** * Save batch record in table * @access public * @param array - all combine data * @return insert id * @author Rajnish Savaliya */ public function saveBatch($collection){ $this-&gt;db-&gt;insert_batch($this-&gt;_table, $collection); return $this-&gt;db-&gt;insert_id(); } /** * Update record in table * @access public * @param array - task data * @param array - field name &amp; value * @return boolean * @author Rajnish Savaliya */ function update($data,$fieldValue = array()) { if(!empty($data) &amp;&amp; !empty($fieldValue)) { $this-&gt;db-&gt;where($fieldValue); $this-&gt;db-&gt;update($this-&gt;_table,$data); if($this-&gt;db-&gt;affected_rows() &gt; 0) return true; else return false; } return false; } /** * Delete record in table * @access public * @param array - field name &amp; value * @return boolean * @author Rajnish Savaliya */ function delete($fieldValue = array()) { if(!empty($fieldValue)) { $this-&gt;db-&gt;delete($this-&gt;_table,$fieldValue); if($this-&gt;db-&gt;affected_rows() &gt; 0) return true; else return false; } return false; } /** * Delete record in table * @access public * @param array - field name &amp; value * @return boolean * @author Rajnish Savaliya */ function delete_multiple($where_in = array(), $fieldName) { if(!empty($where_in)) { $this-&gt;db-&gt;where_in($fieldName,$where_in); $this-&gt;db-&gt;delete($this-&gt;_table); if($this-&gt;db-&gt;affected_rows() &gt; 0) return true; else return false; } return false; } /** * Delete record in table * @access public * @param array - field name &amp; value * @return boolean * @author Rajnish Savaliya */ function update_multiple($data,$where_in = array(), $fieldName) { if(!empty($where_in)) { $this-&gt;db-&gt;where_in($fieldName,$where_in); $this-&gt;db-&gt;update($this-&gt;_table,$data); if($this-&gt;db-&gt;affected_rows() &gt; 0) return true; else return false; } return false; } /** * Get Field or Fields By Id * @access public * @param string - field name * @param number - field id * @return boolean * @author Rajnish Savaliya */ function get_fields($field_names = NULL , $id = NULL) { if($field_names != NULL &amp;&amp; $id != NULL) { $this-&gt;db-&gt;select($field_names)-&gt;from($this-&gt;_table)-&gt;where('id',$id); $query = $this-&gt;db-&gt;get(); $record = $query-&gt;result_array(); if(!empty($record)) { if(count(explode(",", $field_names)) &gt; 1) return $record[0]; else return $record[0][$field_names]; } return ""; } return ""; } /** * Join Two Table * @access public * @param array - result * @return stdClass * @author Rajnish Savaliya */ public function singleJoin($parentTable,$childTable,$select,$condition,$where=array()){ $this-&gt;db-&gt;select($select); $this-&gt;db-&gt;from($parentTable); $this-&gt;db-&gt;where($where); $this-&gt;db-&gt;join($childTable,$condition); return $this-&gt;db-&gt;get()-&gt;result_array(); } /** * Join Two or More Table : mulitple joins with multiple where condition and multiple like condition * @access public * @param array - result * @return stdClass - result * @author Rajnish Savaliya */ public function multijoins($fields,$from,$joins,$where,$ordersby='',$action=NULL,$likes=NULL,$num=NULL,$offset=NULL,$wheretype='where',$groupby=''){ $this-&gt;db-&gt;select($fields); if($wheretype == 'where'){ $this-&gt;db-&gt;where($where); } if($wheretype == 'where_in'){ $this-&gt;db-&gt;where($where); } if($groupby != ''){ $this-&gt;db-&gt;group_by($groupby); } foreach($joins as $key =&gt; $value){ $this-&gt;db-&gt;join($key, $value[0], $value[1]); } if($likes != NULL){ foreach($likes as $field =&gt;$like){ $this-&gt;db-&gt;like($field, $like); } } if($ordersby != ''){ $this-&gt;db-&gt;order_by(''.$ordersby.''); } if($action == 'count'){ return $this-&gt;db-&gt;get($from)-&gt;num_rows(); } elseif($action == 'array'){ return $this-&gt;db-&gt;get($from,$num,$offset)-&gt;result_array(); } else{ return $this-&gt;db-&gt;get($from,$num,$offset)-&gt;result(); } } /** * Join Two or More Table : mulitple joins with multiple where condition and multiple like condition * @access public * @param array - result * @return ArrayObject - result * @author Rajnish Savaliya */ public function multijoins_arr($fields,$from,$joins,$where,$custom_where=NULL,$ordersby='',$num=NULL,$offset=NULL,$action='',$wheretype='where',$groupby=''){ $this-&gt;db-&gt;select($fields); if($wheretype == 'where'){ $this-&gt;db-&gt;where($where); } if($wheretype == 'where_in'){ /*$field = implode(",",(array_keys($where))); $this-&gt;db-&gt;where_in(''.$field.'', $where['p.products_id']);*/ $this-&gt;db-&gt;where($where); } if($groupby != ''){ $this-&gt;db-&gt;group_by($groupby); } foreach($joins as $key =&gt; $value){ $this-&gt;db-&gt;join($key, $value[0], $value[1]); } if($custom_where != NULL){ $this-&gt;db-&gt;where($custom_where); } if($ordersby != ''){ $this-&gt;db-&gt;order_by(''.$ordersby.''); } if($action == 'count'){ return $this-&gt;db-&gt;get($from,$num,$offset)-&gt;num_rows(); }else{ return $this-&gt;db-&gt;get($from,$num,$offset)-&gt;result_array(); } } /** * Function give next/ successor id from calculating ids. * @access public * @param array - result * @return id * @author Rajnish Savaliya */ public function getNextId($tableName,$id='id',$alias='') { if($alias == '') { $alias = $id; } $this-&gt;db-&gt;select_max($id,$alias); $query = $this-&gt;db-&gt;get($tableName); $result = $query-&gt;result_array(); return $result['0'][$alias]+1; } /** * Function check record is exist or not. * @access public * @param array - result * @return boolean true if have dublicate record and false doen't dublicate record * @author Rajnish Savaliya */ public function checkDuplicate($condition,$table=''){ if($table == '') $table = $this-&gt;_table; $query = $this-&gt;db-&gt;get_where($table,$condition); if($query-&gt;num_rows()&gt;=1){ return true; }else{ return false; } } /** * Count Number of record from table * @access public * @Optional = table name * @author Rajnish Savaliya * @return array() */ function count_record($condition,$table='') { if($table == '') $table = $this-&gt;_table; $query = $this-&gt;db-&gt;get_where($table,$condition); return $query-&gt;num_rows(); } /** * Count Number of record from table * @access public * @Optional = table name * @author Rajnish Savaliya * @return array() */ function custom_get($select,$condition = '') { $sql = "SELECT ".$select." FROM ".$this-&gt;_table; if($condition != '') $sql .= " Where ".$condition; $query = $this-&gt;db-&gt;query($sql); return $query-&gt;result_array(); } /** * Retrive all categories from database with tree structure * @access public * @author Pratik Patel * @modified By Rajnish Savaliya * @return array() */ public function get_all_categories() { $refs = array(); $list = array(); $sql = "SELECT iid, cat_code,parent_cat_code, category_name FROM category "; $result = mysql_query($sql); while($data = @mysql_fetch_assoc($result)) { $thisref = &amp;$refs[ $data['cat_code'] ]; $thisref['iid'] = $data['iid']; $thisref['cat_code'] = $data['cat_code']; //$thisref['parent_cat_code'] = $data['parent_cat_code']; $thisref['category_name'] = $data['category_name']; if ($data['parent_cat_code'] == 0) { $thisref['type'] = 'folder'; $list[ $data['cat_code'] ] = &amp;$thisref; } else { $thisref['type'] = 'item'; $refs[ $data['parent_cat_code'] ]['additionalParameters']['children'][ $data['cat_code'] ] = &amp;$thisref; } } return $list; } } </code></pre> <p><strong>HOW TO USE THIS MODEL</strong></p> <p>use is very simple..</p> <p>Load Model: </p> <pre><code> $this-&gt;load-&gt;model("general_model",'general'); </code></pre> <p>Set Table to perform operation:</p> <pre><code> $this-&gt;general-&gt;set_table('category'); </code></pre> <p>set condition</p> <pre><code> $condition = array(); </code></pre> <p>set extra fields like orderby, limit, offset more info you can see in model function 'get()'</p> <pre><code> $ordersby['parent_tag_id'] = 'desc'; </code></pre> <p>Grab the results!!</p> <pre><code> $results = $this-&gt;general-&gt;get("id,name",$condition,$ordersby); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload