In reality, creating methods that build the SELECT MIN, SELECT AVG and SELECT SUM parts of a SELECT SQL query doesn’t differ too much from building the one tasked with constructing a SELECT MAX portion. In this case, the methods will be able to be chained also, since they’ll return an instance of the abstract model class. Now it's time to see how these brand new methods look. Here they are: // Builds the select MIN part of the query public function select_min($field, $alias = '') { if (in_array($field, $this->fields, TRUE)) { $this->db->select_min($field, $alias); } return $this; }
// Builds the select AVG part of the query public function select_average($field, $alias = '') { if (in_array($field, $this->fields, TRUE)) { $this->db->select_min($field, $alias); } return $this; }
// Builds the select SUM part of the query public function select_sum($field, $alias = '') { if (in_array($field, $this->fields, TRUE)) { $this->db->select_min($field, $alias); } return $this; } Certainly, if you take a closer look at the implementation of the above three methods, you’ll realize quickly that they’re simple proxies for their counterparts defined within the CodeIgniter database class (assuming that you’re already pretty familiar with it, of course). So, after creating the appropriate SELECT parts of a query, they’ll return to client code an instance of the “AbstractModel” class, in this way turning themselves into chainable interfacing elements. Not too hard to grasp, right? At this point, it’s fair to say that the model class has became much more functional, especially with the addition of the last three methods. However, the best way to see how these fit with the rest of the class’s code consists of showing the full signature of the class. That’s precisely what I’m going to do in the last section of this tutorial, so go ahead and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|