Menus

Tuesday, 13 August 2019

Codelgniter - Models



What is a Model?

Models are PHP classes that are designed to work with information in your database.
You might have a model class that contains functions to insert, update, and retrieve your blog data.


Anatomy of a Model

Model classes are stored in your application/models/ directory. 
The basic prototype for a model class is this:
class Model_name extends CI_Model {

}
Where Model_name is the name of your class. 
Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name must match the class name. For example, if this is your class:
class User_model extends CI_Model {

}

Loading a Model

Your models will typically be loaded and called from within your controller methods.


$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:
$this->load->model('blog/queries');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->load->model('model_name');

$this->model_name->method();
parameter of the loading method
$this->load->model('model_name', 'foobar');

$this->foobar->method();



Connecting to your Database


  • You can tell the model loading method to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
    $this->load->model('model_name', '', TRUE);
    
  • You can manually pass database connectivity settings via the third parameter:
    $config['hostname'] = 'localhost';
    $config['username'] = 'myusername';
    $config['password'] = 'mypassword';
    $config['database'] = 'mydatabase';
    $config['dbdriver'] = 'mysqli';
    $config['dbprefix'] = '';
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    
    $this->load->model('model_name', '', $config);

Example:

Controller
<?php
class Blog extends CI_Controller {

public function index()
{
//gather information here from models
$this->load->model('blog_model');
echo $this->blog_model->test_blog();
}
}
?>

Model
<?php
class Blog_model extends CI_Model
{
function test_blog()
{
echo "This is model test funtion";
}
//Database function write here
}
?>


Here is an example of what such a model class might look like:

class Blog_model extends CI_Model {

        public $title;
        public $content;
        public $date;

        public function get_last_ten_entries()
        {
                $query = $this->db->get('entries', 10);
                return $query->result();
        }

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

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

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

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

}
Note
The methods in the above example use the Query Builder database methods.
More Details


No comments:

Post a Comment