Home arrow PHP arrow Page 3 - Method Chaining: Adding More Selecting Methods to the CodeIgniter Library

Building the SELECT MIN, SELECT AVG and SELECT SUM parts of a SQL statement with chainable methods - PHP

In this ninth part of an 11-part series on method chaining, I will add three new chainable methods to the custom CodeIgniter model class we built in previous parts. You should find this a straightforward process, especially if you have a decent background in this framework’s database class.

TABLE OF CONTENTS:
  1. Method Chaining: Adding More Selecting Methods to the CodeIgniter Library
  2. Review: the AbstractModel class's source code
  3. Building the SELECT MIN, SELECT AVG and SELECT SUM parts of a SQL statement with chainable methods
  4. The AbstractModel class's updated source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 18, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: