Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use mutliple models depending on wich table i need to work </p> <p>application/core/MY_model.php</p> <pre><code>&lt;?php /** * CRUD Model * A base model providing CRUD, pagination and validation. */ class MY_Model extends CI_Model { public $table; public $primary_key; public $default_limit = 15; public $page_links; public $query; public $form_values = array(); protected $default_validation_rules = 'validation_rules'; protected $validation_rules; public $validation_errors; public $total_rows; public $date_created_field; public $date_modified_field; public $native_methods = array( 'select', 'select_max', 'select_min', 'select_avg', 'select_sum', 'join', 'where', 'or_where', 'where_in', 'or_where_in', 'where_not_in', 'or_where_not_in', 'like', 'or_like', 'not_like', 'or_not_like', 'group_by', 'distinct', 'having', 'or_having', 'order_by', 'limit' ); function __construct() { parent::__construct(); } public function __call($name, $arguments) { call_user_func_array(array($this-&gt;db, $name), $arguments); return $this; } /** * Sets CI query object and automatically creates active record query * based on methods in child model. * $this-&gt;model_name-&gt;get() */ public function get($with = array(), $include_defaults = true) { if ($include_defaults) { $this-&gt;set_defaults(); } foreach ($with as $method) { $this-&gt;$method(); } $this-&gt;query = $this-&gt;db-&gt;get($this-&gt;table); return $this; } /** * Query builder which listens to methods in child model. * @param type $exclude */ private function set_defaults($exclude = array()) { $native_methods = $this-&gt;native_methods; foreach ($exclude as $unset_method) { unset($native_methods[array_search($unset_method, $native_methods)]); } foreach ($native_methods as $native_method) { $native_method = 'default_' . $native_method; if (method_exists($this, $native_method)) { $this-&gt;$native_method(); } } } /** * Call when paginating results. * $this-&gt;model_name-&gt;paginate() */ public function paginate($with = array()) { $uri_segment = ''; $offset = 0; $per_page = $this-&gt;default_limit; $this-&gt;load-&gt;helper('url'); $this-&gt;load-&gt;library('pagination'); $this-&gt;set_defaults(); foreach ($with as $method) { $this-&gt;$method(); } $this-&gt;total_rows = $this-&gt;db-&gt;count_all_results($this-&gt;table); $uri_segments = $this-&gt;uri-&gt;segment_array(); foreach ($uri_segments as $key =&gt; $segment) { if ($segment == 'page') { $uri_segment = $key + 1; if (isset($uri_segments[$uri_segment])) { $offset = $uri_segments[$uri_segment]; } unset($uri_segments[$key], $uri_segments[$key + 1]); $base_url = site_url(implode('/', $uri_segments) . '/page/'); } } if (!$uri_segment) { $base_url = site_url($this-&gt;uri-&gt;uri_string() . '/page/'); } $config = array( 'base_url' =&gt; $base_url, 'uri_segment' =&gt; $uri_segment, 'total_rows' =&gt; $this-&gt;total_rows, 'per_page' =&gt; $per_page ); if ($this-&gt;config-&gt;item('pagination_style')) { $config = array_merge($config, $this-&gt;config-&gt;item('pagination_style')); } $this-&gt;pagination-&gt;initialize($config); $this-&gt;page_links = $this-&gt;pagination-&gt;create_links(); /** * Done with pagination, now on to the paged results */ $this-&gt;set_defaults(); foreach ($with as $method) { $this-&gt;$method(); } $this-&gt;db-&gt;limit($per_page, $offset); $this-&gt;query = $this-&gt;db-&gt;get($this-&gt;table); return $this; } function paginate_api($limit, $offset) { $this-&gt;set_defaults(); if (empty($limit)) { $limit = $this-&gt;default_limit; } if (empty($offset)) { $offset = 0; } $this-&gt;db-&gt;limit($limit, $offset); return $this-&gt;db-&gt;get($this-&gt;table)-&gt;result(); } /** * Retrieves a single record based on primary key value. */ public function get_by_id($id, $with = array()) { foreach ($with as $method) { $this-&gt;$method(); } return $this-&gt;where($this-&gt;primary_key, $id)-&gt;get()-&gt;row(); } public function save($id = NULL, $db_array = NULL) { if (!$db_array) { $db_array = $this-&gt;db_array(); } if (!$id) { if ($this-&gt;date_created_field) { $db_array[$this-&gt;date_created_field] = time(); } $this-&gt;db-&gt;insert($this-&gt;table, $db_array); // $this-&gt;session-&gt;set_flashdata('alert_success', 'Record successfully created.'); return $this-&gt;db-&gt;insert_id(); } else { if ($this-&gt;date_modified_field) { $db_array[$this-&gt;date_modified_field] = time(); } $this-&gt;db-&gt;where($this-&gt;primary_key, $id); $this-&gt;db-&gt;update($this-&gt;table, $db_array); // $this-&gt;session-&gt;set_flashdata('alert_success', 'Record successfully updated.'); return $id; } } /** * Returns an array based on $_POST input matching the ruleset used to * validate the form submission. */ public function db_array() { $db_array = array(); $validation_rules = $this-&gt;{$this-&gt;validation_rules}(); foreach ($this-&gt;input-&gt;post() as $key =&gt; $value) { if (array_key_exists($key, $validation_rules)) { $db_array[$key] = $value; } } return $db_array; } /** * Deletes a record based on primary key value. * $this-&gt;model_name-&gt;delete(5); */ public function delete($id, $others = array()) { if (!empty($others)) { foreach ($others as $k =&gt; $v) { $this-&gt;db-&gt;where($k, $v); } } else { $this-&gt;db-&gt;where($this-&gt;primary_key, $id); } return $this-&gt;db-&gt;delete($this-&gt;table); // $this-&gt;session-&gt;set_flashdata('alert_success', 'Record successfully deleted.'); } /** * Returns the CI query result object. * $this-&gt;model_name-&gt;get()-&gt;result(); */ public function result() { return $this-&gt;query-&gt;result(); } /** * Returns the CI query row object. * $this-&gt;model_name-&gt;get()-&gt;row(); */ public function row() { return $this-&gt;query-&gt;row(); } /** * Returns CI query result array. * $this-&gt;model_name-&gt;get()-&gt;result_array(); */ public function result_array() { return $this-&gt;query-&gt;result_array(); } /** * Returns CI query row array. * $this-&gt;model_name-&gt;get()-&gt;row_array(); */ public function row_array() { return $this-&gt;query-&gt;row_array(); } /** * Returns CI query num_rows(). * $this-&gt;model_name-&gt;get()-&gt;num_rows(); */ public function num_rows() { return $this-&gt;query-&gt;num_rows(); } /** * Used to retrieve record by ID and populate $this-&gt;form_values. * @param int $id */ public function prep_form($id = NULL) { if (!$_POST and ($id)) { $this-&gt;db-&gt;where($this-&gt;primary_key, $id); $row = $this-&gt;db-&gt;get($this-&gt;table)-&gt;row(); foreach ($row as $key =&gt; $value) { $this-&gt;form_values[$key] = $value; } } } /** * Performs validation on submitted form. By default, looks for method in * child model called validation_rules, but can be forced to run validation * on any method in child model which returns array of validation rules. * @param string $validation_rules * @return boolean */ public function run_validation($validation_rules = NULL) { if (!$validation_rules) { $validation_rules = $this-&gt;default_validation_rules; } foreach (array_keys($_POST) as $key) { $this-&gt;form_values[$key] = $this-&gt;input-&gt;post($key); } if (method_exists($this, $validation_rules)) { $this-&gt;validation_rules = $validation_rules; $this-&gt;load-&gt;library('form_validation'); $this-&gt;form_validation-&gt;set_rules($this-&gt;$validation_rules()); $run = $this-&gt;form_validation-&gt;run(); $this-&gt;validation_errors = validation_errors(); return $run; } } /** * Returns the assigned form value to a form input element. * @param type $key * @return type */ public function form_value($key) { return (isset($this-&gt;form_values[$key])) ? $this-&gt;form_values[$key] : ''; } } </code></pre> <p>then when i need a model for a specific database i use class inside models</p> <p>models/content.php</p> <pre><code>class content_types extends MY_Model { function __construct() { parent::__construct(); $this-&gt;curentuserdata = $this-&gt;session-&gt;userdata('lg_result'); $this-&gt;userprofile = $this-&gt;session-&gt;userdata('user_profile'); } public $table = 'content_types'; public $primary_key = 'id'; public $default_limit = 50; public function default_order_by() { $this-&gt;db-&gt;order_by('id asc'); } } </code></pre> <p>then where i need to call the model </p> <pre><code>$this-&gt;modelname-&gt;save($id,array()); $this-&gt;modelname-&gt;where()-&gt;get()-&gt;row(); </code></pre> <p>so create one for the 'view table' and the others for insert tables</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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