How to Structure Models in your MVC based Web Application
MVC
Ever since I started using CodeIgniter about a year and a half ago, I feel that my coding style has drastically improved as well as the speed, security, and reliability of my applications. This is mostly due to CodeIgniter’s MVC approach to the structure of the application. It separates your code into three stages:
- Model – Used to interact with your database or data source and return the data back to your application
- View – Holds all of the display logic such as html, css and javascript
- Controller – Sort of the middleman, used to coordinate all the actions from Models and Libraries into a View to display to the user’s browser
CRUD
CRUD (Create, read, update and delete) is a way of structuring your code that interfaces with your database. Those four actions handle 99% of the actions you will do with the data in your database so why should we duplicate functionality every time we need to do something? For my examples I will be using the Active Record Class in CodeIgniter but it would be just as easy for you to use simple SQL statements in your PHP code.
Ok that’s enough of the vocabulary lesson, lets get into some code.
Start of the Model
All we are doing here is declaring the model as “Blog_model” and we will save this code in blog_model.php in the models folder in Codeigniter.
Create
Creating data is pretty simple, especially in codeigniter. You should only need a single function to create data for each table in your database. This is how i structure my insert functions.
Now what the code above is actually doing is this. When i call $this->whatever_model->insert_blog_post($data); , i pass a $data variable which is actually just an array with the index names matching to the database field names. If you are familiar with CodeIgniter’s Active Record Class this approach is nothing new to you.
Read
Reading information from a database is nothing new. You send a select query to a database and it returns results, how could it get any simpler? Well what usually ends up happening over the development of an application is that the developer will create multiple functions that essentially do the same thing. One function will query the database based on the Unique ID # of an item. Another will query checking to see if a particular title in that blog post already exists. So how can these be combined? Well you can create a single function that actually interacts with the database. It is passed a few parameters that tell it what query to run and it does just that. This function is called by other functions that pass those specific parameters. Here is an example.
Update
Now updating an row (or many rows) in the database is just as easy as adding a row. The first parameter is your where statement which includes the conditions that the rows must have to be updates. The second parameter is the data that will be inserted into those selected rows.
Pretty simple right? Now all you have to do from anywhere in your application to update a row in blog_posts is $this->blog_model->update_blog_post(array(‘ID’ => 5), array(‘title’ => ‘Getting Started with CRUD’));
Delete
Just as you might suspect, deleting a post is just as simple as adding or updating a post.
Now just run the command $this->blog_model->delete_blog_post(array(‘ID’ => ’223′));
Thats it!
If you have any questions or comments be sure to leave them below!

Nice article, Tom. You could add a further level of abstraction by passing the table name as a parameter form the controller.
And maybe there’s a little typo on the “read” function: the parameter passed in line #1 should be named $data as the param in line #3.
Bye, and keep up with nice posts,
Andreagam
Good catch there Andrea. Its fixed now. As for the table parameter i find that to get “too abstracted” and it gets to the point where it is just like codeigniter’s active record class, and then if you change a table name for instance you have to go to 20 functions and change that parameter instead of one function.
You could also make the table name an object :
public $table = ‘blog_posts’;
Then in your model $this->db->update($this->table, $data);
Then your problem of table name change is solved, you’ll have only 1 variable to change on top of your model.
@toopixel
That’s another approach to this. For each project you have to make a decision on what is best for your work flow and application. Sometimes a more abstracted approach allows for more flexibility. Sometime you need a little less so that you don’t confuse other developers in the long run.