2

I was pushed into an existing CodeIgniter project and am familiarizing myself with the proper conventions for their MVC approach. One thing I'm stuck on is relating models and controllers. The CI documentation gives almost no guidance on this and I'm not sure what the correct approach is. Our project is an API which is called by external applications so I'm not concerned with views, only models and controllers.

This is an off-the-cuff example rather than a practical example, but hopefully it will help me understand: Let's say that I'm making an application that has minigames and a virtual store. The player can earn points by playing minigames. They can spend those points in the shop to buy items. They can also sell items in the shop to earn points. The application should log each transaction of points.

In this situation, my thinking is I would have a:

  • ShopModel - interacts with the database to load product data and to save what items the user has purchased
  • MiniGameModel - interacts with the database to load data about the minigames and save scores
  • PointsModel - interacts with the database to transfer points into or out of the player's account (and writes corresponding log entries in the "log" table)

  • ShopController - utilizes ShopModel and PointsModel

  • MiniGameController - utilizes MiniGameModel and PointsModel

So, for example, the user tells the application they want to search the shop for "weapons". The application requests weapons from the controller. The shop controller builds a query and the shop model executes the query and returns the records. The controller formats the records as JSON and sends them back to the application.

The user tells the application they want to buy the shield. The application notifies the shop controller. The shop controller queries the price from the shop model, then tells the points model to subtract the points, then tells the shop model to give the player the shield. Finally, the controller notifies the application via JSON that the shield was purchased.

Is this an acceptable structure, or should each controller only have one model? If there should only be one model per controller, how do I avoid copy-pasting code that handles the points? Am I doing this completely wrong?

Please do not get hung up on the database structure or anything like that. This is not a real example. I'm just trying to figure out how to organize classes.

2
  • 1
    You're definitely on the right track. Keep the logical segments separated and stay DRY. Commented Jun 24, 2015 at 21:39
  • 1
    you created model perfectly (each for a separate tasks), you can use multiple models in a controller, and also a model in multiple controllers. Commented Jun 25, 2015 at 8:15

2 Answers 2

2

It's really as simple as you're thinking. Models in Codeigniter are just supposed to passed db results to the controller, or custom class, then to the controller.

You can load as many models as you like in a controller - thats not a problem at all. Personally I try to keep my model > db table in a one to one relationship. If I have work to do on the data from the model, i'll usually add a Library (custom class) to handle that part.

That way my controller stays clean.

Sign up to request clarification or add additional context in comments.

Comments

1

i would give you some suggestion :

  1. To save your time use this API RESTSERVER for functionally like you explain.

  2. For common functionality make a library, then auto load it and when ever you need it just called it where you want.you can called a library every where ,in view,controller,model .

e.g for just an idea, Library which would look like.

<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

    class Menu {

        public $ci;
        public $table="tblmenu";
        public $full_record = array();
        function __construct() {
            $this->ci = &get_instance();
            $this->company_id = $this->ci->session->userdata('company_id');
        }
        public function get_menu($comp_id = NULL) {
            $comp_id = ($comp_id) ? $comp_id : $this->company_id;
            $this->ci->db->select()->from($this->table)->where('company_id', $comp_id);
            $this->ci->db->where('visible', 1);
            $this->ci->db->order_by('id', 'ASC');
            $this->full_record = $this->ci->db->get()->result_array();
            return $this->full_record;
        }
    }

Then when you need it :

$this->load->library('menu');//or auto load it .
$this->menu->get_menu();

2 Comments

I have gotten the strong impression that the convention for CI is to only access the database from models. Is that impression inaccurate?
no ! it's not necessary ,you can make use of libraries and helper function all together to done work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.