Home arrow PHP arrow Page 3 - Adding Ordering and Grouping Clauses to the CodeIgniter Library with Method Chaining

Conditional queries: adding WHERE modifiers to a SELECt statement - PHP

Welcome to the tenth installment of a series on method chaining in PHP 5. Comprised of twelve tutorials, this series teaches you the key concepts that surround the implementation of chainable methods within PHP classes, and shows how to put them to work to create a custom library for the popular CodeIgniter framework.

TABLE OF CONTENTS:
  1. Adding Ordering and Grouping Clauses to the CodeIgniter Library with Method Chaining
  2. Creating the JOIN, ORDER BY and GROUP parts of a query
  3. Conditional queries: adding WHERE modifiers to a SELECt statement
  4. The custom model class's full source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In reality, itís pretty easy to add to the previous custom model class a few additional chainable methods that build several WHERE parts of a query. To demonstrate, below I included the definitions of these concrete methods, so take some time to examine them closely.

Here they are:

// Builds the WHERE part of the query using AND and other operators

public function get_where($where, $protect_identifiers = TRUE)

{

if ((is_string($where) OR is_array($where)) AND !empty($where))

{

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

}

return $this;

}

 

// Builds the WHERE part of the query using OR and other operators

public function get_or_where($where, $protect_identifiers = TRUE)

{

if ((is_string($where) OR is_array($where)) AND !empty($where))

{

$this->db->or_where($where, $protect_identifiers);

}

return $this;

}

 

// Builds the WHERE IN part of the query

public function where_in($field, $values)

{

if (in_array($field, $this->fields, TRUE) AND is_array($values) AND !empty($values))

{

$this->db->where_in($field, $values);

}

return $this;

}

 

// Builds the WHERE NOT IN part of the query

public function where_not_in($field, $values)

{

if (in_array($field, $this->fields, TRUE) AND is_array($values) AND !empty($values))

{

$this->db->where_not_in($field, $values);

}

return $this;

}

 

// Builds the OR WHERE NOT IN part of the query using the OR operator

public function or_where_not_in($field, $values)

{

if (in_array($field, $this->fields, TRUE) AND is_array($values) AND !empty($values))

{

$this->db->or_where_not_in($field, $values);

}

return $this;

}

Undoubtedly, understanding how the above methods work should be pretty straightforward, since theyíve been implemented similarly to the methods that you learned in the previous segment.

Of course, in this particular case they behave as wrappers for their equivalents in the CodeIgniter database class, which makes it really easy to dynamically add several query modifiers, like WHERE, WHERE NOT IN, OR WHERE, and so forth to a given SQL query.

Assuming that you already grasped the logic that drives the previous chainable methods, itís time to list the whole source code of the abstract model class, this time including the definitions of the methods in question.

This will be done in the last section of this tutorial. Therefore, go ahead and read the final segment. Itís only one click away.



 
 
>>> 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: