Home arrow PHP arrow Page 3 - Building a Sample Application with Method Chaining and CodeIgniter

Adding functionality to the Users controller class - PHP

Definitely, building chainable class methods with PHP 5 is a process that can be mastered with minor effort, even for developers with only an average level of experience in using the object-oriented paradigm. Therefore, if you're a passionate PHP programmer who wishes to learn the key concepts that surround the implementation of method chaining in a painless fashion, then this group of articles is what you need.

  1. Building a Sample Application with Method Chaining and CodeIgniter
  2. Review: the abstract model class so far
  3. Adding functionality to the Users controller class
  4. Finishing the controller class with update and delete methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
December 03, 2009

print this article



As I stated in the previous segment, I'm going to implement the first two methods of the "Users" controller to retrieve all of the users stored on the MySQL table, and to insert a new one.

That being explained, here's how these methods now look:

// list all users

public function index()


$data['users'] = AbstractModel::factory('User')->order_by('id')->fetch();

$data['title'] = 'User List';

$data['heading'] = 'List of available users';

$this->load->view('users_view', $data);



// save a new user

public function save()


$user = AbstractModel::factory('User');

$user->firstname = 'Wendy';

$user->lastname = 'Torrance';

$user->email = 'wendy@torrance.com';

if ($user->save())


redirect ('/users');


$this->load->view('error_view', array('error' => $user->error));


As shown above, the "index()" method demonstrates how helpful the chainable methods of the custom model can be. It uses only one line of code to fetch all the users from the corresponding MySQL table. In this case, I decided to call the "factory" method to create an instance of the model, but you may want to directly use its constructor instead.

Once the index method has retrieved the data from the MySQL table, it's directly embedded into a new view file called "users_view," for displaying purposes. Later on, I'm going to list the definition of this file, so don't worry about it for the moment.

Now, regarding the definition of the "save()" method, it first creates a new user, then assigns to the object the corresponding properties, and finally saves this data to the database table. Also, you may have noticed that, again, I used the "factory()" method to create a new user.

Why did I do this, instead of creating the user object only once? I did it only to keep the code of each method uncluttered. But to do things more correctly, an instance of the user model should be created in the constructor and stored as a property of the controller, thus making it accessible to all of its methods.

Now that you hopefully understood how the previous methods do their things, it's time to code the remaining ones, which will be tasked with updating and deleting an existing user.

This process will be discussed in the last section of this tutorial. So click on the link below and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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