aid

Wednesday, 15 April 2015

Creating Models in MVC

Creating Models in MVC
Models are available for those who want to use a MVC approach.
Models are classes and functions that designed to toil with in sequence within your database. For example, let's say you use CodeIgniter that is follow MVC approach to manage a web application. You capacity have a model class and model function and function contains  insert, update, and retrieve your data from database. Belo example show how to look model classes:


class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

No comments:

Post a Comment