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

Finishing the controller class with update and delete methods - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The last two methods of the "Users" controller that still have no concrete implementation are the ones responsible for updating and deleting users respectively. Therefore, it's necessary to turn them into functional structures quickly.

Based on this requirement, below I included the definitions of these methods, which look like this:

// update an existing user

public function update()

{

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

$user->firstname = 'Daniel';

$user->lastname = 'Torrance';

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

$user->id = 1;

if ($user->save())

{

redirect ('/users');

}

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

}

 

// delete an existing user

public function delete()

{

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

$user->id = 1;

if ($user->delete())

{

redirect ('/users');

}

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

}

Certainly, the methods shown above should be very easy for you to grasp, since they look quite similar to the ones coded in the previous section. In this case, once a particular user has been successfully updated or deleted, a redirection will take place, and the list of users will be automatically refreshed.

If, for some reason, these operations fail, an error message will be displayed on screen by using the following view file:

('error_view.php' file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>An error was found!</title>

</head>

<body>

<h2>The following error occurred:</h2>

<?php echo $error;?>

</body>

</html>

In addition, here's the view responsible for displaying the list of users stored on the MySQL database table:

('users_view.php' file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>User List</title>

</head>

<body>

<h1><?php echo $heading;?></h1>

<?php foreach ($users->result as $user):?>

<p><?php echo 'First Name: ' . $user->firstname;?></p>

<p><?php echo 'Last Name: ' . $user->lastname;?></p>

<p><?php echo 'Email: ' . $user->email;?></p>

<hr />

<?php endforeach?>

</body>

</html>

And with this last code sample, the development of this sample database-driven application is finished. Of course, as you may have guessed, it's possible to define additional methods inside the controller that display, for instance, the full details of a specified user.

To do this in a simple manner, you might want to use some of the chainable methods provided by the generic model class and build the correct query dynamically. But this will be left as homework for you. Meanwhile, feel free to play with all of the code samples included in this tutorial, to give yourself a better background in using method chaining in PHP 5.

Final thoughts

It's hard to believe, but we've come to the end of this series. In these 12 tutorials you've hopefully learned how to define and implement chainable methods in PHP 5.

Ranging from building simple classes that parse strings and abstract accesses to MySQL, to developing a full-featured custom model class for CodeIgniter, method chaining is a powerful approach that will help you to create compact -- yet readable -- class APIs with minor effort.

See you in the next PHP development 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: