Welcome Guest, Not a member yet? Register   Sign In
The use of models
#1

[eluser]tobben[/eluser]
How do you guys deal with models?

Do you try to use a function on as many queries as possible?

Like using the same function for a lot of stuff..

Code:
function select($table, $where=NULL, $where_ref=NULL, $orderby=NULL, $ascdesc=NULL, $limit=NULL) {

$query = $this->db->get($table); // etc...

}

Or do you make a new model function for each individual query?
#2

[eluser]sikkle[/eluser]
To be honest, most of the time i create several function, all in one function usually bring me some probleme, and always re-ask myself ok how why what.

But i mean, i'm pretty sure on the long run, all in one function could do the job.

see ya around.
#3

[eluser]Lone[/eluser]
Generally speaking we tend to send variables from a post/uri as a single variable and try to leave as much of the intepretation upto the model - that way if we need to replicate the process in a controller there is minimal code in the controller/s and all in the model.

eg. For a add/edit form we would send the whole 'post' contents the controller receives to the model.
#4

[eluser]tobben[/eluser]
Yes, thet last one I do. I'm just trying to figure out a smart solutions, since my model class is as long as seven bad years..

Could probably make one model for each application, instead of putting all-in-one.. heh
#5

[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);
    }
}

?&gt;
#6

[eluser]gon[/eluser]
Using Active record you can avoid passing single
variables.

A function to store data could be like this:

Code:
function store($data) {
    $this->db->set($data);
    if (isset($data['id']) {
        return $this->update("table_name");
    } else {
        $this->db->insert("table_name");
        return $this->db->insert_id();
    }
}

If you modify the model fields, the function doesn't need to be changed.
You'll probably have to change the previous $data validation though.




Theme © iAndrew 2016 - Forum software by © MyBB