model
section which reduces efforts for developers. Here, active records reduces the burden of writing query syntax, by calling functions with proper parameters sequentially, which when executed gives a full length SQL query and returns respective results of database. This helps in not doing mistakes while writing long SQL queries.
Another advantage of using CodeIgniter Active Records is it can create database independent applications, since the query syntax is generated by each adapter of database. As the system automatically escapes the values, safe queries are inputted. WHERE
clause helps in extracting only the records that satisfy the given condition. For Example,
[php]
$query = $this->db->get_where('thetable', array('id' => $id));
// or //
$query = $this->db->get_where('thetable', array('id' => $id), $limit, $offset);
[/php]
The function where()
enables to set WHERE clauses using one of 4 methods which helps to run the query multiple times and then $this->db->get();
can be used to send the query.
For example,
[php]$this->db->where('name', $name); [/php]
Output will be as follows:
[php]WHERE name = ‘Rick’[/php]
When multiple function calls are occurs, the result will be grouped together with AND
between them.[php]
$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
[/php]
Output will be as follows:
[php]WHERE name = ‘Rick’ AND title = ‘Rock Star’ AND status = ‘Legend’[/php]
website
with the fields of website_name
, website_url
and website_description
. Below is the code to insert the values into database table using active record $this->db->insert();.
[php]
$data = array(
'website_name' => 'SPLessons' ,
'website_url' => 'http://www.splessons.com' ,
'website_description' => 'Stop Thinking, Start Coding'
);
$this->db->insert('website', $data);
[/php]
Output will be as follows:
[php]
INSERT INTO website (website_name, website_url, website_description) VALUES ('SPLessons', 'http://www.splessons.com', 'Stop Thinking, Start Coding')
[/php]
In the above example, a set of values that are related to each other in an array are taken, and inserted that data into one row of database table that has 3 fields. But multiple groups of data can also be inserted at a time using function $this->db->insert_batch();
.
[php]
$data = array(
'website_name' => 'SPLessons' ,
'website_url' => 'http://www.splessons.com' ,
'website_description' => 'Stop Thinking, Start Coding'
),
array(
'website_name' => 'Google' ,
'website_url' => 'http://www.google.com' ,
'website_description' => 'The Google Search Engine Homepage'
)
);
$this->db->insert_batch('website', $data);
[/php]
The output produced will be as follows:
[php]
INSERT INTO website (website_name, website_url, website_description) VALUES ('SPLessons', 'http://www.splessons.com', 'Stop Thinking, Start Coding'), ('Google', 'http://www.google.com', 'The Google Search Engine Homepage')
[/php] delete()
when required to delete data from multiple tables.
[php]
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);
[/php] exists()
will be used for this functionality.
[php]
function view($name)
{
$data['page'] = $this->Page->find_by_name($name);
if ($data['page']->exists())
{
$this->load->view('single_page', $data);
}
else
{
show_404();
}
}[/php]