In this article, I will give a small overview of the Codeigniter Active Record class, show the basic way to use it, its drawbacks and potential solution of a common problem(union operation with it, which isn’t directly supported) that developers face often. I will be using a few possible PHP code examples for demonstration purposes also, wherever applicable.
So, you are working with database functionality in Codeginiter framework. So, I guess, you already know some basics? If yes, go ahead, no problem. But if your answer is ‘NO’? Why don’t walk through a codeigniter basic application development tutorial first? It will make your life easier for doing the exercises of this tutorial.
What is CodeIgniter Active record class:
Among many others, the “Active record class” is one of the most popular and useful features of Codeigniter. It reduces the developer’s effort to create and execute SQL queries on ‘model’ section. Instead of writing query syntax, here we have to call functions with proper parameters sequentially, which, when executed, results in a full-length SQL query and returns corresponding database results. This is a great help, as while writing ourselves, sometimes, for long/complex queries, we have to correct SQL syntax several times before getting it functional.
Using This Class:
Using this active record class in a Codeigniter application is quite easy. Remember not to use this on the controller or view section(not even in library/plugin/helpers also) as this actually belongs to the model section(data access layer). Here are few PHP code samples are given below:
functionget_result($id){
//For selecting one or more columns$this->db->select('title, content, date');
//For determine one or more tables to select from$this->db->from('table1');
//For joining with another table, table name as first argument and condition string as second argument$this->db->join('table2', 'comments.id = blogs.id');
//assign where condition$this->db->where('id', $id);
//function without any argument. It actually runs the query that was built on last few statements.$this->db->get();
//returns result objects arrayreturn $query->result();
}
Code language:PHP(php)
Note that, on ‘where’ method, you can pass an associative array as parameters as well. That will assume an ‘AND’ operator in every key-value (key = DB column name, value = result to be checked against) pair and construct the query accordingly. Inserting data is also quite easy, as follows:
For more details references on exactly which functions are allowed, and what parameters with details Explanation with an example, please visit official documentation on it.
Do you know? How to see what query is actually getting constructed? You can get it by using “$this->db->last_query()” method after execution. We are gonna use it soon, keep reading. Also, if you keep your profiler enabled by “$this->output->enable_profiler(TRUE);”, then you should see the executed query, time taken etc benchmark at the bottom of the page.
Actually, if we talk about finding a way to create SQL queries easier, then its do the work. But, for better development or if we want to follow another technology like ORM(Object-relational mapper), then it won’t fulfill our needs. Moreover, it also doesn’t provide support for long-range database engines(create different SQL syntax for a different database like MySQL, SQL server, oracle etc). In this case, we will just need to get help from third-party tools like doctrine, which are easily integrable with Codeigniter as a plugin/library.
Also, another drawback(or should I say lacking enhancement?), the active record class doesn’t provide any function to perform “UNION” operation directly, at least, I didn’t find any(Correct me if I am wrong).
Perform Union operation using active record class:
Although most of the functions are provided as alternatives to SQL syntax, like select() for ‘SELECT’, where() for ‘WHERE’, there was no implemented method for UNION operation. So, if you are going to need to execute a query that requires ‘UNION’ , you may be thinking about what to do, avoid using an active record class. If not, and its possible to use it there then how?
If you are thinking for performance, best is to avoid active record and write raw query. But, if you are not comfortable in writing raw query and looking for a codeigniter active record based alternative solution, there is one for you as well.
Although it’s not possible to use ‘UNION’ directly, still, we can implement it with the use of an active record class. Here is how: Let’s assume we are merging two different columns into one in the same database table(One of the possible reasons why we need to use UNION). The code should be something like this:
Here, what we are doing is simply getting help from the CodeIgniter active record class to prepare two different queries and then we are manually constructing our original query. That’s all. The above function will return the correct results with desired UNION operation.
Let me know(by commenting) if you found something more which aren’t implemented properly or if you have any other question related to this so that I can start finding a solution to them and update this post by adding them and solution PHP code samples also. Happy coding 🙂
Hi, Thanks for your feedback. Yes, I do know also that its not a quite good practice. However, it can be a work around for developers who doesn’t feel comfortable or doesn’t know to write raw sql query. I better hope that, codeigniter active record class do get a new release with this features in near future. Additionally, as you suggested I will mention it in the original post.
Thank you for sharing this information. I did implement this code in my script, but I didn’t manage to get the expected results since I am using 2 databases from 2 different hosts / servers. I think, this only works to manipulate databases from the same server. I will be very much appreciated if you can help me to figure out how to union two databases from two different server. Once again, thank you very much.
I think, in that case, It will be best to retrieve them individually and then merge them using php itself(with array_merge($array1, $array2) function). Hope this helps.
thank you so much for this useful teaching about codeigniter, i have an issue with a graph to display with codeigniter, my sql request cannot work when i use codeigniter however it works when i don’t use it, how hard i tried to make it work with codeigniter i fail. this code is about: $query = mysqli_query($GLOBALS[“___mysqli_ston”], “Select monthname(TranDate) as MyDate, sum(T.Newark) as Newark,
sum(T.Berkeley) as Berkeley, sum(T.Elizabeth) as Elizabeth
From
(SELECT TranDate, Sum(Production) as Newark, 0 as Berkeley, 0 as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND
oid=1007
group by month(TranDate)
having sum(production)>0
Union
SELECT TranDate, 0 as Newark, Sum(Production) as Berkeley, 0 as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND oid=1008
group by month(TranDate)
having sum(production)>0
Union
SELECT TranDate, 0 as Newark, 0 as Berkeley, sum(Production) as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND oid=1009
group by month(TranDate)
having sum(production)>0
Hi Md Ali Ahsan Rana, see if you can help me with this code, this model only taking the start date and the end date zeroing, follows the code so you can help me:
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Nipop says
Nice work. Simple and works like a charm!!!
ev45ive says
The problem is – it runs 3 queries – which 2 of has no use, except to compile sql statement. Bit of a waste, isn’t it?
I know that CI_DB_active_record::_compile_select() is a private function, but still bit of a overkill.
~ev45ive
Asanka says
+1
You might as well write the query as a string. Why run 2 additional queries to get the string output?
@author – I suggest you correct this as it can lead a newbie towards another bad practice.
Md Ali Ahsan Rana says
Hi, Thanks for your feedback. Yes, I do know also that its not a quite good practice. However, it can be a work around for developers who doesn’t feel comfortable or doesn’t know to write raw sql query. I better hope that, codeigniter active record class do get a new release with this features in near future. Additionally, as you suggested I will mention it in the original post.
Laurah Misaha says
Thank you for sharing this information. I did implement this code in my script, but I didn’t manage to get the expected results since I am using 2 databases from 2 different hosts / servers. I think, this only works to manipulate databases from the same server. I will be very much appreciated if you can help me to figure out how to union two databases from two different server. Once again, thank you very much.
Rana says
I think, in that case, It will be best to retrieve them individually and then merge them using php itself(with array_merge($array1, $array2) function). Hope this helps.
Baptiste Prophete says
thank you so much for this useful teaching about codeigniter, i have an issue with a graph to display with codeigniter, my sql request cannot work when i use codeigniter however it works when i don’t use it, how hard i tried to make it work with codeigniter i fail. this code is about: $query = mysqli_query($GLOBALS[“___mysqli_ston”], “Select monthname(TranDate) as MyDate, sum(T.Newark) as Newark,
sum(T.Berkeley) as Berkeley, sum(T.Elizabeth) as Elizabeth
From
(SELECT TranDate, Sum(Production) as Newark, 0 as Berkeley, 0 as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND
oid=1007
group by month(TranDate)
having sum(production)>0
Union
SELECT TranDate, 0 as Newark, Sum(Production) as Berkeley, 0 as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND oid=1008
group by month(TranDate)
having sum(production)>0
Union
SELECT TranDate, 0 as Newark, 0 as Berkeley, sum(Production) as
Elizabeth
FROM OpenDent.production
Where
year(TranDate) = year(now()) AND oid=1009
group by month(TranDate)
having sum(production)>0
) as T
Group By month(T.TranDate)”);
$category = array();
$category[‘name’] = ‘MyDate’;
$series1 = array();
$series1[‘name’] = ‘Newark’;
$series2 = array();
$series2[‘name’] = ‘Berkeley’;
$series3 = array();
$series3[‘name’] = ‘Elizabeth’;
while($r = mysqli_fetch_array($query)) {
$category[‘data’][] = $r[‘MyDate’];
$series1[‘data’][] = $r[‘Newark’];
$series2[‘data’][] = $r[‘Berkeley’];
$series3[‘data’][] = $r[‘Elizabeth’];
}
$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
array_push($result,$series3);
print json_encode($result, JSON_NUMERIC_CHECK);
((is_null($___mysqli_res = mysqli_close($con))) ? false : $___mysqli_res);
i have issue to separate these codes in model file and controller file.. would you help me please!
Alex Xavier Rosa says
Hi Md Ali Ahsan Rana, see if you can help me with this code, this model only taking the start date and the end date zeroing, follows the code so you can help me:
VIEW
echo form_open(‘agenda/createcons’, ‘class=”form-cadastro1″‘);
//$field_array = array(‘Data do Exame’, ‘Horario’, ‘Nome Paciente’, ‘Medico Solicitante’);
echo heading($headline, 3, ‘class=”form-cadastro-heading”‘);
echo br();
echo form_input(‘data_inicial’, ”, ‘id=”data_inicial” title=”Data Inicial da Pesquisa” class=”input-block-level input-xlarge data_inicial” placeholder=”Data Inicial” required’);
echo form_input(‘data_final’, ”, ‘id=”data_final” title=”Data Inicial da Pesquisa” class=”input-block-level input-xlarge ” placeholder=”Data Final”‘);
echo br();
echo br();
echo form_submit(”, ‘Consultar’, ‘class=”btn btn-primary”‘);
echo form_close();
MODEL
function getAgendaFil()
{
echo $data_fini = date(‘Y-m-d’, strtotime($this->input->post(‘data_final’)));
echo $data_ini = date(‘Y-m-d’, strtotime($this->input->post(‘data_inicial’)));
echo $data_fini = date(‘Y-m-d’, strtotime($this->input->post(‘data_final’)));
print_r($this->db->get_where(‘agenda’,’data_consulta BETWEEN “‘. $data_ini. ‘” and “‘. $data_fini.'”‘));
$this->db->order_by(‘data_consulta,hora’,”asc”);
return $this->db->get(‘agenda’);
}
Md Ali Ahsan Rana says
“only taking the start date and the end date zeroing”
– Sorry, I didn’t get your question. Please explain clearly with expected/actual output as well.
mathirajesh says
Thank you for your help.