[eluser]Seppo[/eluser]
I usually define a model per entity with 6 basic methods, 5 public (fetchlist, get, count, save, delete), 1 private (parse_filters) and one or many classes related to that model that will store the information, can have some small logic, but nothing DB related, in that case they'll contact the model.
An example of a model I used for an own small site.
Code:
<?php
class Mymodel extends Model {
function Mymodel() {
parent::Model();
}
function fetchlist($object_type,$filters = array()) {
$this->db->from($object_type);
$this->parsefilters($object_type,$filters);
$query = $this->db->get();
$object_type = "obj_" . $object_type;
$fields = $query->list_fields();
$return = array();
foreach ($query->result() as $row) {
$object = new $object_type;
foreach ($fields as $f)
$object->$f = $row->$f;
$return[] = $object;
}
return $return;
}
function get($object_type,$filters = array()) {
$filters['start'] = 0;
$filters['limit'] = 1;
$result = $this->fetchlist($object_type,$filters);
$object_type = "obj_" . $object_type;
if (empty($result)) return new $object_type;
else return current($result);
}
function count($object_type,$filters = array()) {
$filters['start'] = 0;
$filters['limit'] = 1;
$this->db->select("count(*) AS q");
$this->db->from($object_type);
$this->parsefilters($object_type,$filters);
$query = $this->db->get();
$row = $query->row();
return $row->q;
}
function save(&$object) {
$this->db->set($object);
$tabla = substr(get_class($object),4);
if (empty($object->id)) {
$this->db->insert($tabla);
$object->id = $this->db->insert_id();
} else {
$this->db->where('id',$object->id);
$this->db->update($tabla);
}
}
function delete(&$object) {
$tabla = substr(get_class($object),4);
$this->db->delete($tabla,array('id' => $object->id));
unset($object);
}
function parsefilters($object_type,$filters = array()) {
foreach ($filters as $k => $v) {
if (in_array($k,array('start','orderbydir'))) continue;
switch (true) {
case ($k == "limit"):
$this->db->limit($v,intval(isset($filters['start']) ? $filters['start'] : 0));
break;
case ($k == "orderby"):
$this->db->orderby($v,(isset($filters['orderbydir']) ? $filters['orderbydir'] : 'ASC'));
break;
case ($k == 'search'):
$this->db->where('(noticia LIKE "' . $this->db->escape_str('%' . $v . '%') . '" OR titulo LIKE "' . $this->db->escape_str('%' . $v . '%') . '")');
break;
case ($k == "hasta" && $object_type == 'noticia'):
$this->db->where('fecha <=',$v);
break;
case ($k == "desde" && $object_type == 'noticia'):
$this->db->where('fecha >=',$v);
break;
default:
$this->db->where($k,$v);
break;
}
}
}
}
class obj_comentario {
var $id = 0;
var $id_noticia = 0;
var $nombre = "";
var $mensaje = "";
function save() {
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
class obj_foto {
var $id = 0;
var $descripcion = "";
var $foto = "";
var $fecha = "";
function obj_foto() {
$this->fecha = date('Y-m-d');
}
function save() {
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
class obj_guestbook {
var $id = 0;
var $nombre = "";
var $mensaje = "";
var $fecha = "";
function save() {
if (empty($this->fecha)) $this->fecha = date('Y-m-d H:i:s');
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
class obj_invitado {
var $id = 0;
var $nombre = "";
var $email = "";
var $viene = "";
var $comentarios = "";
function save() {
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
class obj_noticia {
var $id = 0;
var $fecha = "";
var $titulo = "";
var $noticia = "";
var $autor = "";
function save() {
if (empty($this->fecha)) $this->fecha = date('Y-m-d H:i:s');
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
class obj_regalo {
var $id = 0;
var $nombre = "";
var $reservado = "";
function save() {
$CI =& get_instance();
return $CI->mymodel->save($this);
}
function delete() {
$CI =& get_instance();
return $CI->mymodel->delete($this);
}
}
?>