As I said in the previous section, the last step that I’m going to take in this tutorial will consist of adding two core methods to the “AbstractModel” class, for performing select and insert/update SQL statements. The respective implementations of these methods are shown below, so pay close attention to them: // Fetches rows from specified table public function fetch($limit = NULL, $offset = NULL) { $data = array(); foreach ($this->fields as $field) { if (isset($this->$field) AND $this->$field != '') { $data[$field] = $this->$field; } } $query = !empty($data) ? $this->db->get_where($this->table, $data, $limit, $offset) : $this->db->get($this->table, $limit, $offset); if ($query->num_rows() > 0) { $this->result = $query->result(); return $this; } $this->error = 'No rows were returned.'; return FALSE; }
// Inserts/updates a row into the specified database table public function save() { $data = array(); foreach ($this->fields as $field) { if (isset($this->$field)) { $data[$field] = $this->$field;
} } // if there is any data available go ahead and save/update row if( !empty($data)) { // validate input data if ($this->validate($data) === FALSE) { $this->error = $this->get_error_string(); return FALSE; } // if id property has been set in the controller update existing row if ( !empty($this->id)) { // Update existing record $this->db->where('id', $this->id); $this->db->update($this->table, $data); } else { // otherwise insert new row $this->db->insert($this->table, $data); $this->id = $this->db->insert_id(); } return TRUE; } $this->error = 'No valid data was provided to save row.'; return FALSE; } From the previous code samples, it’s pretty clear to see how the methods defined above do their things. In the first case, the “fetch()” method is responsible for retrieving database records from the table associated with the model, via the database class provided by CodeIgniter. If you’re not familiar with it yet, I recommend you read its user guide. It's worth noting that this particular method is capable of performing “limited” SELECTS if the proper offset and limit arguments are specified, and according to the given conditions, it’ll store in a “result” property the data set retrieved from the pertinent table. Of course, an example of the use of this property will be developed in upcoming parts of this series, so for the moment you don't need to worry about it. The other method, called “save(),” will simply insert a new record into the specified database table, or update an existing one if the corresponding ID has been specified. In addition, it’s fair to note that the supplied data will be validated via a “validate()” method before performing the previous operations, but again, the implementation of this method will be covered in a subsequent article of the series. At this stage you should have a clearer idea of how to use the method chaining approach for creating a custom library for CodeIgniter. My final suggestion is that you to study the methods shown before in detail, since this process may take a while. Final thoughts That’s all for the moment. In this seventh part of the series, I hopefully demonstrated how method chaining can be used for developing some core methods of a custom library for CodeIgniter. In the following article, I’m going to keep adding more chainable methods to this library, which will equip it with the functionality required for building different modifiers of SELECT statements. Want to learn how these useful methods will be defined? Then don’t miss the next tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|