Codeigniter CRUD: Day 1 Create Model

Active Record | Codeigniter | Crud | Database | Model | Mysql

Codeigniter CRUD Day 1


This entry is part 1 of 6 in the series CodeIgniter CRUD

Codeigniter CRUD is a most requested tutorial for Learn CodeIgniter course. It is an overview to CRUD operations. CRUD stands for Create Read Update and Delete. We’ll see how easily we can develop a CRUD using Codeigniter’s active record queries.

We will Divide this tutorial into 5 parts:

  1. Create Database Table & respective model
  2. Create Operation
  3. Read Operation
  4. Update Operation
  5. Delete Operation

Prerequisites for Codeigniter CRUD Tutorial:

If you don’t know any of above, I highly recommend you to please refer to above tutorials first. Then follow CodeIgniter CRUD tutorial.

Let’s Create Codeigniter CRUD

Overview: We will create a simple module of post table’s 4 CRUD operations. Which is create, read, update & delete using Codeigniter’s Active Record class.

Step 1: Create a database in mysql and add table named post by executing following query.

Step 2: Set codeigniter database configuration file to point to your database. In my case database name is ci and it is in localhost.

Step 3: Create a model file inside application/models directory and name it postmodel.php. write following code for model functionalities.

Here we created a class with exactly same name as our model filename with first letter capital. In our case its Postmodel class which extends CI_Model base class of Codeigniter. Next we created constructor method and call parent class’s constructor. If you like you can remove it but i will put it as is in case we want to put anything in constructor. We also created a instance variable $table which stores table name for model. So we don’t have to change everywhere incase we change table name. Then we created a instance method getAll() which fetches all data of post table from database using active record. Here we return blank array incase post table is empty.

Second instance method is add() which has one argument. Which is an array of data needs to be inserted. add() function has only one line of code which do the insertion part for post table using passed data in argument.

Third instance method is update() which has 2 arguments. First is an array of data needs to be updated. Second is id for which data needs to be updated. Here we used database class’s where method to specify id for which data needs to be updated.

Forth and final instance method is delete() which has only one argument. Which is id of row that needs to be deleted. similar to update we have 2 line code for delete.

So we have our database table and model ready for Codeigniter CRUD. So let’s move forward to Create operation for Codeigniter CRUD. You can download Full source code till Codeigniter CRUD: Day 1 Create Model from below link.


Download Source Code Files

Please like our Facebook Fan page for instant updates on Tutorials.

Series NavigationCodeigniter CRUD: Day 2 Create Operation >>

Bookmark Link using: bookmark at folkd