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’ 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.
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:
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.
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:
the qualities you are embeddings have been gotten away in the background by this code:
At the end of the day, the CI system is making your code more powerful.
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:
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:
$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:
$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):
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:
At that point, if there are various results, they can be returned as a $row object through which you emphasize with a foreach loop:
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:
$row = $query->row_array();
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 .