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!
blog comments powered by Disqus |
|
|
|
|
|
|
|