Codeigniter Models
Models are optionally accessible for people who need to use an additional ancient MVC approach.
Models ar PHP categories that are designed to figure with data in your information.
For example, let’s say you utilize CodeIgniter to manage a weblog.
You might have a model category that contains functions to insert, update, and retrieve your weblog knowledge.
Here is an associate example of what such a model category may look like:
<?php
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 ways within the on top of example use the question Builder information ways.
Note - For the sake of simplicity during this example we’re exploitation $_POST directly.
This is generally a bad practice, and a more common approach would be to use the Input Library $this->input->post('title').
Anatomy of A Model
Model classes are stored in your application/models/ directory. They can be nested within sub-directories if you want this type of organization.
The basic paradigm for a model category is this:
<?php
class Model_name extends CI_Model {
}
Where Model_name is the name of your class.
Class names should have the primary letter capitalized with the remainder of the name little.
Make sure your category extends the bottom Model category.
The file name must match the class name. For example, if this is your class:
<?php
class User_model extends CI_Model {
}
How to load a model
Your models can generally be loaded and known as from inside your controller strategies.
To load a model you may use the subsequent method:
If your model is found during a sub-directory, embrace the relative path from your models' directory.
For example, if you've got a model situated at application/models/blog/Queries.php you’ll load it using:
Once loaded, you may access your model strategies mistreatment AN object with constant name as your class:
$this->model_name->method();
If you'd like your model appointed to a unique object name you'll be able to specify it via the second parameter of the loading method:
$this->foobar->method();
Here is AN example of a controller, that hundreds a model, then serves a view:
<?php
class Blog_controller extends CI_Controller {
public function blog()
{
$this->load->model('blog');
$data['query'] = $this->blog->get_last_ten_entries();
$this->load->view('blog', $data);
}
}
How to load the model automatically?
If you discover that you just want a specific model globally throughout your application, you'll be able to tell CodeIgniter to auto-load it throughout system format.
This is done by the gap of the application/config/autoload.php file and adding the model to the autoload array.
Connecting to Databases
When a model is loaded it doesn't connect mechanically to your info.
The following choices for connecting square measure out there to you:
-
You may be connected using the standard database methods described here, either from within your Controller class or your Model class.
-
you'll be telling the model loading technique to auto-connect by passing TRUE (boolean) via the third parameter, and property settings, as outlined in your info config file can be used:
You can manually pass info property settings via the third parameter
<?php
$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);