Home arrow PHP arrow Page 3 - Expanding a Custom CodeIgniter Library with Method Chaining

The chainable select() and select_max() methods - PHP

Welcome to the eighth installment of a series on method chaining in PHP 5. With numerous code samples, this series shows you how to define chainable methods within your own PHP classes. Best of all, it teaches you how to implement this powerful programming method in a real-world case: developing an abstract model for the CodeIgniter framework.

TABLE OF CONTENTS:
  1. Expanding a Custom CodeIgniter Library with Method Chaining
  2. Review: the delete() method
  3. The chainable select() and select_max() methods
  4. The AbstractModel class's full source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 16, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

It would be useful to provide the previous “AbstractModel” class with the capacity to separately build the SELECT and SELECT MAX parts of a SQL query. To do so, I’m going to create a couple of chainable methods that will perform this task in a truly modular way.

Essentially, these methods are simple proxies for their counterparts defined within the CodeIgniter database class, and their respective implementations are as follows:

// Builds SELECT part of the query

public function select($select = '*', $protect_identifiers = TRUE)

{

if ($select != '*' AND !empty($select))

{

$select = explode(',', $select);

foreach ($select as $key => $field)

{

if ( !in_array($field, $this->fields, TRUE))

{

unset($select[$key]);

}

}

$select = !empty($select) ? $select : '*';

}

$this->db->select($select, $protect_identifiers);

return $this;

}

 

// Builds the select MAX part of the query

public function select_max($field, $alias = '')

{

if (in_array($field, $this->fields, TRUE))

{

$this->db->select_max($field, $alias);

}

return $this;

}

Undoubtedly, the custom model class is starting to take shape with the addition of the two chainable methods defined previously. As you can see, the first one will build the SELECT portion of a SQL query, while the last one will create the SELECT MAX part, making it possible to chain them with others very easily.

Well, at this stage you hopefully learned how to apply the method chaining approach to extending the functionality of the previous custom model class for CodeIgniter. In the last part of this article I’m going to list the complete source code of the model, this time including the additional methods created today.

What are you waiting for? 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 6 - Follow our Sitemap

Dev Shed Tutorial Topics: