Here is an improvement suggestion:
With this improvement you can use one model for all your code and you don’t need to create a separate model for each database.
I use a class that I call the Master_Model see below.
I usually still have stuff in the custom model but the thinks you need in every model or in many models are in the Master_Model class.
Model:
class Bb_Model extends Master_Model
function __construct()
{
parent::Model();
$this->mdb->initdb();
}
function findWhat($what_rules = NULL) {
if($what_rules !== NULL){
$this->what= " $what_rules ";
}else{
$this->what= ' * ';
}
}
$sql = 'SELECT '.$this->what.' FROM '. $this->table . $this->where . $this->limit;
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row)
{
$row = delids($row);
$results[] = $row;
}
}
Controller:
function __construct() {
$this->table = 'bb_event';
$this->load->model('Master_Model', 'mdb');
$this->load->model('bb_model', 'bbm')
}
function browse() {
$this->bbm->findWhat('Title, Postcode, Starts, Ends, Active');
}
Master_Module
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Master_Model extends Model {
public function setTable($master_table)
{
$this->table = $master_table;
return $this->table;
}
public function checkTable()
{
If( ! isset($this->table) ){
redirect('dbview/pages/error_db', 'location');
}else{
return $this->table;
}
}
public function initdb($id = FALSE)
{
if($id !== FALSE){
$this->dbdata = $this->get_by_id($id);
}else{
$fields = $this->db->list_fields($this->table);
foreach ($fields as $field) $this->dbdata[$field] = '';
}
return $this->dbdata;
}
public function get_by_id($id, $table = FALSE)
{
If( $table !== FALSE ){ $this->table = $table; }
$this->checkTable();
$return = array();
$query = $this->db->query("SELECT * FROM `" .$this->table. "` WHERE id = '" .$id. "' LIMIT 1");
if ($query->num_rows() > 0) {
$return = $query->row_array();
}else{
$return = FALSE;
}
return $return;
}
Following I have in a helper file, could also be part of the Master_Model but I keep it as helper in case one day I have a model which does not need the Master_Model so that I still have this array helper available. Hasn’t happened so far.
Helper:
<?php
/************************** START UNSET/DEL IDS ****************************
* Removes id, *_id and also first and last date fields from database array
****************************************************************************/
function delids($array)
{
$array = (array) $array;
if(isset($array[0])){
foreach($array as $k => $v){
$array[$k] = unsetids($v);
}
}else{
$array = unsetids($array);
}
return $array;
}
function unsetids($array)
{
$keys = array_keys($array);
foreach($keys as $v){
if($v=='id')unset($array[$v]);
if($v=='last')unset($array[$v]);
if($v=='first')unset($array[$v]);
if($v=='added')unset($array[$v]);
if($v=='updated')unset($array[$v]);
if(strstr($v, '_id'))unset($array[$v]);
}
return $array;
}
/************************** END UNSET/DEL IDS ****************************/