Home / Web Development Technologies / Codeigniter / Utilizing CI to Simplify Databases

Utilizing CI to Simplify Databases

CI to Simplify Databases

CI to Simplify Databases

You’re looking at CI since you need to make coding less demanding and more productive. This part is about CI’s Active Record class. On the off chance that CI offered just its Active Record class, it would in any case be justified regardless of each penny of the price tag.

Okay, it’s free. I’ll reword that—it would in any case be a major tool to increment your productivity. Active Record permits you to handle databases with at least whine and a most extreme of clarity. It’s easy to utilize and keep up.

Active Record

‘Active Record’ is an ‘design pattern’— another of those very unique frameworks such as MVC, which give templates to taking care of normal coding issues furthermore create a portion of the bluntest books on the planet.

In itself, it isn’t code, only an pattern for code. There are a few distinct interpretations of it. At its center is the creation of a relationship between your database and an object, each time you do a query.

Ordinarily, every table is a class, and every single line turns into an object. Every one of the things you might need to do with a table line—make it, read it, upgrade it, or erase it, for example—become to be ‘methods’, which that object acquires from its class. Ruby on Rails is constructed around the Active Record design, as is CI—in spite of the fact that the definite execution in the two framework appears to have subtle differences.

Enough hypothesis—what does it mean? Indeed, basic and clear code statements, if you don’t mind putting arrows in them.

Preferences of Using the Active Record Class

Active record spares you time, gets programmed usefulness that you don’t have to consider, and makes SQL statements straightforward.

Saving Time

When you compose an ordinary database query in PHP, you should compose an association with the database every time. With CI, you associate once to the database, by putting the following line in the constructor function of each controller or model:

$this->load->database();

Once you’ve done this, you don’t need to rehash the connection, what number of ever queries you then make in that controller or model.
You set up the database details in the config files. Once again, this makes it easier to update your site, if you ever change the database
name, password, or location.

Automatic Functionality

Once you’ve associated with the database, CI’s Active record syntax brings shrouded code with it. Case in point, on the off chance that you enter the accompanying “insert” query:

              $data = array(
                              ‘title’ => $title,
                              ‘name’ => $name,
                              ‘date’ => $date
              );
              $this->db->insert(‘mytable’, $data);

the qualities you are embeddings have been gotten away in the background by this code:

            function escape($str)

            {

                    switch (gettype($str))
                       {case ‘string’:
                       $str = “‘”.$this->escape_str($str).”‘”;
                       break;
                       case ‘boolean’: $str = ($str === FALSE) ? 0 : 1;
                       break;
                       default : $str = ($str === NULL) ? ‘NULL’ : $str;
                       break;
                      }
            return $str;
          }

At the end of the day, the CI system is making your code more powerful.

Read Queries

The most widely recognized query that we’ll compose basically recovers data from the database as indicated by our criteria. The fundamental guideline to perform a read query is:

$query = $this->db->get(‘Persons’);

This is a ‘SELECT *’ query on the Persons table—as it were, it recovers all the fields. In the event that you want to indicate the objective table (Persons) in a different line, you can do so in this way:

$this->db->from(‘sites’);
$query = $this->db->get();

In the event that you need to “SELECT” or confine the quantity of fields recovered, as opposed to get them all, utilization this guideline:

$this->db->select(‘url’,’CustomerName’,’Customerid’);
$query = $this->db->get(‘Persons’);

You might need to display the outcomes in a specific request—say by the Persons name—in
which case you add (before the $this->db->get row):

$this->db->orderby(“name”, “desc”);

desc implies in descending request. You can likewise pick asc(ascending) or rand(random).

Displaying/Showing Query Results

Indicating database inquiry results in CI is entirely basic. We characterize our question as above,finishing in:

$query = $this->db->get();

At that point, if there are various results, they can be returned as a $row object  through which you emphasize with a foreach loop:

foreach ($query->result() as $row)
{
print $row->url;
print $row->name;
print $row->client;
}

on the other hand on the off chance that we just need a solitary result, it can be returned as an article, or here as a $row cluster:

if ($query->num_rows() > 0)
{
$row = $query->row_array();
print $row[‘url’];
print $row[‘name’];
print $row[‘client’];
}

By and by, I incline toward the item sentence structure to the array—less typing!When you take after the MVC design, you will as a rule need to keep your questions and database connections in models, and show the data through perspective.

Thanks for reading CI to Simplify Databases .

CI to Simplify Databases You're looking at CI since you need to make coding less demanding and more productive. This part is about CI's Active Record class. On the off chance that CI offered just its Active Record class, it would in any case be justified regardless of each penny of the price tag. Okay, it's free. I'll reword that—it would in any case be a major tool to increment your productivity. Active Record permits you to handle databases with at least whine and a most extreme of clarity. It's easy to utilize and keep up. Active Record 'Active Record' is an…

0%

User Rating: 5 ( 1 votes)

Check Also

MongoDB Features and future

MongoDB Features and Future

MongoDB Features and Future. Welcome to the world of MongoDB. This article delivers a brief …

Non Relational Database or NoSql

Non Relational Database OR NoSQL

Non Relational  Database OR NoSQL Definition – What does Non Relational Database or NoSQL mean? …

Leave a Reply

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