Home arrow PHP arrow Page 4 - Building a CodeIgniter Custom Library with Method Chaining

Fetching, inserting and updating database records - PHP

Welcome to the seventh part of a 12-part series on the technique of method chaining in PHP 5. In this part, I will demonstrate how method chaining can be used to develop some core methods of a custom library for CodeIgniter.

TABLE OF CONTENTS:
  1. Building a CodeIgniter Custom Library with Method Chaining
  2. Defining the CodeIgniter custom library's first methods
  3. Extending the functionality of the AbstractModel class
  4. Fetching, inserting and updating database records
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 11, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the previous section, the last step that I’m going to take in this tutorial will consist of adding two core methods to the “AbstractModel” class, for performing  select and insert/update SQL statements.

The respective implementations of these methods are shown below, so pay close attention to them:  

// Fetches rows from specified table

public function fetch($limit = NULL, $offset = NULL)

{

$data = array();

foreach ($this->fields as $field)

{

if (isset($this->$field) AND $this->$field != '')

{

$data[$field] = $this->$field;

}

}

$query = !empty($data) ? $this->db->get_where($this->table, $data, $limit, $offset) : $this->db->get($this->table, $limit, $offset);

if ($query->num_rows() > 0)

{

$this->result = $query->result();

return $this;

}

$this->error = 'No rows were returned.';

return FALSE;

}

 

// Inserts/updates a row into the specified database table

public function save()

{

$data = array();

foreach ($this->fields as $field)

{

if (isset($this->$field))

{

$data[$field] = $this->$field;

 

}

}

// if there is any data available go ahead and save/update row

if( !empty($data))

{

// validate input data

if ($this->validate($data) === FALSE)

{

$this->error = $this->get_error_string();

return FALSE;

}

// if id property has been set in the controller update existing row

if ( !empty($this->id))

{

// Update existing record

$this->db->where('id', $this->id);

$this->db->update($this->table, $data);

}

else

{

// otherwise insert new row

$this->db->insert($this->table, $data);

$this->id = $this->db->insert_id();

}

return TRUE;

}

$this->error = 'No valid data was provided to save row.';

return FALSE;

}

From the previous code samples, it’s pretty clear to see how the methods defined above do their things. In the first case, the “fetch()” method is responsible for retrieving database records from the table associated with the model, via the database class provided by CodeIgniter. If you’re not familiar with it yet, I recommend you read its user guide.

It's worth noting that this particular method is capable of performing “limited” SELECTS if the proper offset and limit arguments are specified, and according to the given conditions, it’ll store in a “result” property the data set retrieved from the pertinent table. Of course, an example of the use of this property will be developed in upcoming parts of this series, so for the moment you don't need to worry about it. 

The other method, called “save(),” will simply insert a new record into the specified database table, or update an existing one if the corresponding ID has been specified. In addition, it’s fair to note that the supplied data will be validated via a “validate()” method before performing the previous operations, but again, the implementation of this method will be covered in a subsequent article of the series.

At this stage you should have a clearer idea of how to use the method chaining approach for creating a custom library for CodeIgniter. My final suggestion is that you to study the methods shown before in detail, since this process may take a while.

Final thoughts

That’s all for the moment. In this seventh part of the series, I hopefully demonstrated how method chaining can be used for developing some core methods of a custom library for CodeIgniter.

In the following article, I’m going to keep adding more chainable methods to this library, which will equip it with the functionality required for building different modifiers of SELECT statements.

Want to learn how these useful methods will be defined? Then don’t miss the next tutorial!



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