Active Record in CodeIgniter

CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

Selecting data
All of the functions in this section will build SQL SELECT queries. All of the SQL in this section is MySQL; other database systems may differ slightly.

$this->db->get();
The simplest query that you can do with Active Record is to select a full database table. This is done with one single function:

This would create the SQL query:

This function has three parameters. The first is the name of the database table. The second lets you set a limit, and the third lets you set an offset.

This would then produce the SQL query:

$this->db->get_where();

This function works in much the same way as the previous function. The only difference is that the second parameter should be passed as an array. The array should have the name of the field and the value to use to fill in the WHERE part of your query.

This would produce the following SQL query:

$this->db->select();

This function allows you to write the SELECT portion of your query. Take a look at the following example:

The SQL query produced from this function will be :

You should take note that when using this function, and any of the other functions that let you write a portion of your query, that you still need to use the get() function to actually produce and run the query.
If you are selecting everything form your database (*) then you do not need to use this function as CodeIgniter assumes that you mean to select everything.

$this->db->from();
This function allows you to write the FROM portion of your query. This is basically the same as using the get() function, although it is slightly more readable. You can use whichever method you prefer.

$this->db->join();
This function lets you write the JOIN part of your query. Here’s an example:

You can specify a different type of join in the third parameter. You can choose from left, right, outer, inner, left outer, and right outer.

$this->db->where();
This function is used to build the WHERE portion of your query. This function can be used in a variety of ways.
Single key or value method

Multiple key or value method

Associative array method
You can pass values to the where() function by using an associative array. Take a look at the following example:

You can include operators in the array, just as you would use in the first parameter.

$this->db->like();
This function allows you to write the LIKE portion of your query, and functions in almost the exact same way as the where() method.
Single key or value method
This method is the same as the where() method, when used as follows:

Multiple key or value method
This method is also the same as the where() method. Multiple calls will be chained together.

$this->db->group_by();
This function lets you write the GROUP BY portion of your query.

$this->db->order_by();
This lets you write the ORDER BY portion of your query. The first parameter is for your field name. The second is the type of order that you want to use, and can be asc, desc, or random.

You can also pass a string as the first parameter.

Multiple function calls can also be used, as for other functions.

$this->db->limit();
This function lets you add a LIMIT specification to your query. The first parameter will be the number to limit to, and the second parameter let’s you set an offset.

Inserting data
Inserting data using Active Record is a very simple process, and there are just two functions that you may need to use in order to insert data into your database.

This will generate an insert string based upon the data that you supply to it. The first parameter is the name of the table that you want to add the data to, and the second parameter can either be an array or an object of the data.

This would then produce the following SQL statement:

$this->db->update();
This will generate an update string based upon the data that you supply to it.
The first parameter is the name of the table you want to add the data to, and the second parameter can either be an array or an object of the data. The third,
optional parameter enables you to set the WHERE clause of your SQL query.

This would then produce the following SQL statement:

You can optionally use the where() method to set the where clause of the query. Here’s how you would use the third parameter to set the WHERE clause:

$this->db->delete();
This function accepts two parameters. The first is the name of the table, and the second should be an array from which to build the WHERE clause.

You can also use the where() function to build the WHERE clause:

An array of table names can be passed into this function, if you wish to delete more than one table.

Source https://www.codeigniter.com/userguide2/database/active_record.html

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *