Search code examples
phpcodeigniterdatabase-abstraction

Where is my MODEL rule here in CodeIgniter system?


I'm using codeIgniter as my framework to build a very big website. Everything is fine. I use Models to get/send data from/to database. But a question has recently confused me. In getting data from loops throughout the page, I don't use the Models.

For instance, for the right sidebar I use a loop to get the list of categories. Now, this loop is based upon an array, and this array is a set of data retrieved from Database. This data-retrieval from DB is processed in the controller. Is this CORRECT? Or I should absolutely get any data from DB only and only in the MODELS? Since I only use Models for registrations/logins/newsletters/orders etc.

Here is one of my controller which also is responsible for getting DB data:

$this->niazer-> is a library which is responsible to get database data (solely made for my current project)

<?php
/**
    This controller is the basic controller to render the webpages. Now we have the 
**/
class show extends CI_Controller
{
       function index()
        {                                       

            /** getting list of categories **/

            $cats = $this->niazer->get_pa('all'); // getting all the parents
            $x= $this->niazer->get_child($cats);    
            $data['main_cats'] = $cats;     
            /** getting list of categories **/

            /** getting list of special ads with 5-7 stars **/
            $special_ads_var = $this->niazer->get_ads(array("star-min"=>5, "star-max"=>7, "row"=>5)); // getting all the parents
            $data['special_ads'] = $special_ads_var;    
            /** end of special ads list **/


            /** getting list of special ads with 5-7 stars **/
            $special_ads_var = $this->niazer->get_news(array("limit"=>20)); // getting all the parents




            $theme_name = $this->theme->get_theme_with_slash(false);                                    

            $this->load->view($theme_name.'header', $data);
            $this->load->view($theme_name.'index', $data);
            $this->load->view($theme_name.'footer', $data);

        }

}

Solution

  • If it's about good practice and principles then you are doing it wrong, IMO.

    The model gives the controller a data representation of whatever the user requested (a message, a list of books, a photo album, etc). This data model will be the same no matter how you may want to present it to the user, that’s why you can choose any available view to render it. The model contains the most important part of your application logic, the logic that applies to the problem you are dealing with (a forum, a shop, a bank, etc).

    The controller contains a more internal-organizational logic for the application itself (more like housekeeping).

    I can't write about these things here but I think this answer covers the things that you are looking for.

    To be honest, in my early days of programming with CI I didn't even use a MODEL but my applications worked well but now I follow the rules, there are obviously good reasons to follow rules and principles (SOLID). So, you can do it like you are doing it now, none will force you.