Codeigniter CRUD Model Code Generator
Simple tool to generate Create, Read, Update, Delete code for Codeigniter Model
Table Name (Plular):
Table Name (Singular):
Table Alias:
PK Key:
Columns:
customer_id, customer_name, order_by, status
Model Code:
function get_max_{tbl_name_singular}_id() { $this->db->select_max('{pk_field}', '{pk_field}'); $q = $this->db->get('{tbl_name_plular}'); $result = $q->result_array(); $result = $result[0]; if($result['{pk_field}'] == null) { ${pk_field} = 1; } else { ${pk_field} = $result['{pk_field}']+1; } return ${pk_field}; } function get_{tbl_name_plular}_result($search_options = array(), $limit = 0, $offset = 0) { $this->db->select(" {select} "); $this->db->from('{tbl_name_plular} {tbl_alias}'); {select_wheres} if(isset($search_options['order_by'])) { $this->db->order_by($search_options['order_by']); } else { $this->db->order_by('{tbl_alias}.{pk_field} DESC'); } if($limit != 0) { if($offset == 0) { $this->db->limit($limit); } else { $this->db->limit($limit, $offset); } } $q = $this->db->get(); return $q; } function get_{tbl_name_plular}($search_options = array(), $limit = 0, $offset = 0, $total_rows = 0) { if(empty($search_options)){ return array(); } $search_options['limit'] = $limit; $search_options['offset'] = $offset; $search_options['total_rows'] = $total_rows; $result = array(); $q = $this->get_{tbl_name_plular}_result($search_options, $limit, $offset); if($q->num_rows() > 0) { $row_num = $total_rows - $offset; foreach($q->result_array() as $row) { $row['row_num'] = $row_num; $row_num--; $result[] = $row; } } return $result; } function store_{tbl_name_singular}(${tbl_name_singular}_info) { $result = false; if(!empty(${tbl_name_singular}_info)) { $this->db->set(${tbl_name_singular}_info); $result = $this->db->insert('{tbl_name_plular}'); } return $result; } function update_{tbl_name_singular}(${tbl_name_singular}_info) { $result = false; $has_condition = false; if(!empty(${tbl_name_singular}_info)) { if(isset(${tbl_name_singular}_info['{pk_field}']) && ${tbl_name_singular}_info['{pk_field}'] != '') { $this->db->where('{pk_field}', ${tbl_name_singular}_info['{pk_field}']); unset(${tbl_name_singular}_info['{pk_field}']); $has_condition = true; } if($has_condition == false){ return false; } $this->db->set(${tbl_name_singular}_info); $result = $this->db->update('{tbl_name_plular}'); } return $result; } function delete_{tbl_name_plular}($search_options = array()) { $result = false; $has_condition = false; if(!empty($search_options)) { if(isset($search_options['{pk_field}']) && $search_options['{pk_field}'] != '') { $this->db->where('{pk_field}', $search_options['{pk_field}']); unset($search_options['{pk_field}']); $has_condition = true; } if($has_condition == false){ return false; } $result = $this->db->delete('{tbl_name_plular}'); } return $result; }
if(isset($search_options['{field_name}'])) { $this->db->where('{tbl_alias}.{field_name}', $search_options['{field_name}']); }