How to create Models in CodeIgniter 3 easy way.

Codeigniter Models

Introduction to CodeIgniter Models

Models in CodeIgniter are an essential part of an application, generally, we create a model for every table. Well before getting more into the models let’s understand a little bit about Codeigniter. It is a popular PHP framework that allows developers to create web applications quickly and easily. One of the most important components of any web application is its database, and CodeIgniter provides a simple way to interact with databases through its models.

A model is essentially a PHP class that is responsible for communicating with the database. Models in CodeIgniter follow the Model-View-Controller (MVC) design pattern, which separates the application logic into three distinct components. Models are the “M” in MVC, and they handle all the data-related logic.

Creating models and directory location

To create a model in CodeIgniter, you need to create a new PHP file in the “application/models” directory with the name of your model. For example, if you want to create a model for a blog post, you would create a file called “Post_model.php”. The file should contain a class with the same name as the file, and it should extend the CodeIgniter Model class. Here’s an example:

<?php
/**
 * Post_model is extending core model (CI_Model) of the codeigniter
 */
class Post_model extends CI_Model {

}

Once you’ve created your model, you can use it to interact with the database. CodeIgniter provides several methods that you can use to perform CRUD operations on your data. Here are some of the most common methods:

  • $this->db->get('table_name'): Returns all the rows from the specified table.
  • $this->db->get_where('table_name', array('column_name' => 'value')): Returns rows from the specified table that match the specified conditions.
  • $this->db->insert('table_name', $data): Inserts a new row into the specified table.
  • $this->db->update('table_name', $data, array('column_name' => 'value')): Updates one or more rows in the specified table that match the specified conditions.
  • $this->db->delete('table_name', array('column_name' => 'value')): Deletes one or more rows from the specified table that match the specified conditions.

Example of methods in Codeigniter Models

Here’s an example of how you might use these methods in your model:

<?php
class Post_model extends CI_Model {

    public function get_posts() {
        return $this->db->get('posts')->result();
    }

    public function get_post($id) {
        return $this->db->get_where('posts', array('id' => $id))->row();
    }

    public function create_post($data) {
        $this->db->insert('posts', $data);
        return $this->db->insert_id();
    }

    public function update_post($id, $data) {
        $this->db->update('posts', $data, array('id' => $id));
    }

    public function delete_post($id) {
        $this->db->delete('posts', array('id' => $id));
    }

}

In this example, the model has five methods: get_posts(), get_post($id), create_post($data), update_post($id, $data), and delete_post($id). The get_posts() the method returns all the posts from the “posts” table, the get_post($id) the method returns a single post with the specified ID, the create_post($data) the method inserts a new post into the “posts” table, the update_post($id, $data) method updates an existing post with the specified ID, and the delete_post($id) method deletes a post with the specified ID.

Using models in Controllers

Once you’ve created your model, you can use it in your controller to retrieve data from the database and pass it to the view for display. Here’s an example of how you might use the Post_model in a controller:

<?php
class Post_controller extends CI_Controller {

    public function index() {
        $this->load->model('post_model');
        $data['posts'] = $this->post_model->get_posts();
        $this->load->view('posts', $data);
    }

    public function view($id) {
        $this->load->model('post_model');
        $data['post'] = $this->post_model->get_post($id);
        $this->load->view('post', $data);
    }

    public function create() {
        $this->load->model('post_model');
        $data = array(
            'title' => 'My New Post',
            'content' => 'This is the content of my new post.',
        );
        $this->post_model->create_post($data);
    }

    public function update($id) {
        $this->load->model('post_model');
        $data = array(
            'title' => 'Updated Post Title',
            'content' => 'This is the updated content of my post.',
        );
        $this->post_model->update_post($id, $data);
    }

    public function delete($id) {
        $this->load->model('post_model');
        $this->post_model->delete_post($id);
    }

}

In this example, the Post_controller has five methods: index(), view($id), create(), update($id), and delete($id). The index() the method retrieves all the posts from the Post_model and passes them to a view called “posts”. The view($id) method retrieves a single post with the specified ID from the Post_model and passes it to a view called “post”. The create() method creates a new post using the Post_model. The update($id) method updates an existing post with the specified ID using the Post_model. The delete($id) method deletes a post with the specified ID using the Post_model.

In conclusion, models are an essential component of any CodeIgniter application. They allow developers to interact with the database and perform CRUD operations on data. By following the MVC pattern and creating a separate model for each database table, you can keep your code organized and maintainable.

You can read more models in detail at https://codeigniter.com/userguide3/general/models.html