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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: