Lynda. Up and Running with PHP CodeIgniter / Section 3 - 3. Saving magazines using active records

  • 10 years ago
Saving magazines using active records

One of the convenient and time saving features of CodeIgniter is a loose implementation of the active record design pattern. When I use CodeIgniter's active records, I don't need to create classes to connect to each database table, but I should create a model for each database table. And I can use active records to make the models perform database operations. This system is database agnostic, meaning all the query syntax is generated automatically. I can access CodeIgniter's database using $this->db followed by commands such as insert Update and delete. Rather than explicitly breaking down each command, I will demonstrate by example, by updating the models to allow them to be persistent.

I am also going to extend the bare minimum of native CodeIgniter model class to be more functional by adding generic CRUD functionality to it. This approach will also demonstrate how to extend the CodeIgniter functionality. If I wanted to generically extend a CodeIgniter class, a specific naming convention is required. Instead of ci_, use my_. I'll switch to NetBeans and navigate to the Core folder. I'm going to extend the CI model class, so, I'll create a new file named, my_model.php.

Class, my_model extends CI_model. Now, your initial reaction may be to make this class abstract. I'll be honest, that's what I wanted to do first. But CodeIgniter instantiates these classes and abstract classes can't be instantiated. Therefore, it'll just have generic functionality that won't work until it's extended. I'll start by adding a constant containing the name of the database table that'll store records of a given model. This is very helpful for organization and documentation as well. I have to set a value, so, I'll just make it abstract, and just override it. I will define a second constant containing the primary key for the table, const DB_TABLE_PK equals abstract.

Next, I am to going to create the four CRUD methods. I will start with create, which will be an insert method. I will make it private as I'm going to have a more unifying save method that will determine whether to call insert or update. That's in documentation, create record. I'm going to use this DB to access active record, then the method insert, which takes two arguments. The name of the table then questioned as a string.

And then object containing the arguments that mapped to table columns that will be written, which will just be this. The insert operation will not manually populate anything, so, I will have to manually populate the model with the insert ID. Fortunately there's a method for that. This curly brace this DB_TABLE_PK equals this db, and then the method, insert_id. That's all that' needed. And let's do a more complex one, which is the update method, which I'll also make private, so, I can call it from the unified save method. Private function update.

Then give a documentation, update record. This -> db -> update, takes three arguments: the name of the table to update, which will be this db table, and an array or object containing the columns or values to update, which will just be this. And the primary key of the table that will be updated. This DB_TABLE_ PK, I'll use the constants defined earlier to make a generic. To prepare for a loading method, I'm going to make a helper that will populate the model from an array or standard class. Which will take one argument, a mixed row.

So, public function populate. And I'll take a row. So, populate from an array or standard class, param mixed row. And then for each row as key value, this dollar sign key equals value. Next, I'll implement a method for loading a single record from the database into a model that takes one argument. I'll call it public function load, which'll take one argument, the ID of the record.

(NOISE) Load from the database. Param will be an integer containing the primary key. In this method, I will assign a variable called query to the result of this -> db -> get_where, which retrieves a standard object with a result. It takes two arguments, which is the table name again, DB_TABLE followed by an array of the key value pairs that it will be matched against. Which in this case will be just the primary key and the ID I want to look up. This DB_TABLE_PK, assign to ID.

I'll use the previously created populate method to populate the object. So, this -> populate with query -> row. The final method of CRUD is delete: public function delete. Delete the current record, this -> db -> delete. The delete method takes the name of the table, this DB_TABLE followed by an array similar to the where statement I used in load.

Recommended