{"id":321,"date":"2008-05-11T13:40:08","date_gmt":"2008-05-11T18:40:08","guid":{"rendered":"http:\/\/unitstep.net\/?p=321"},"modified":"2008-05-11T18:27:16","modified_gmt":"2008-05-11T23:27:16","slug":"cakephp-and-errorexception-handling","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2008\/05\/11\/cakephp-and-errorexception-handling\/","title":{"rendered":"CakePHP and error\/exception handling"},"content":{"rendered":"
<\/p>\n
I’m currently using CakePHP<\/a> and finding it to be quite useful. The “automagic” handling of tables is useful for basic relationships, though more complicated setups usually require manual work. The MVC implementation has also clearly drawn inspiration from Ruby on Rails<\/a>, which may be advantageous to some, though this has no bearing on me. Though there are a few things that nag me about CakePHP (such as lack of a good testing suite, though that’s supposedly fixed in 1.2<\/a>, which really should be marked as version 2.0), overall it’s a great framework that adds badly-needed structure to PHP<\/acronym> and has saved me time.<\/p>\n One thing I’d like to see, however, is a proper exception handling model. I realize this would require making it PHP<\/acronym> 5-only<\/a>, but in my opinion, PHP<\/acronym> 5 adds some sorely-need features, such as the aforementioned exception handling model and a class\/object system more in line with other languages.<\/p>\n <\/p>\n I’ve written about the benefits of exception handling<\/a> before, so I won’t delve into those details again. In short, using exceptions is a better way for a method to signal to a caller that something went wrong rather than just returning a special\/reserved value as an error code.<\/p>\n In particular, saving of models would benefit from this. Currently, if you want to check if the model was saved properly, you have to do something like this:<\/p>\n It’s a little bit kludgy, but isn’t actually that bad since Cake simplifies form validation a lot with the HTML<\/acronym> helper in views. You can simply put something like However, in some situations you might want to execute your own logic when a call to Coming from a Java background, this makes more sense to me, since I consider its exception handling model to be one of the language’s strong points. It has numerous benefits to improving readability and maintainability of code.<\/p>\n However, there are some drawbacks, since this requires the use of PHP<\/acronym> 5. As CakePHP is designed to be PHP<\/acronym> 4 and 5 compatible, exception handling is really not an option. However, since PHP<\/acronym> 4 is so old, I’d eventually like to see a move to PHP<\/acronym> 5<\/a>, especially for its better OOP model and exception handling. Additionally, PHP<\/acronym> 6 is just around the corner, so I don’t really see a need to stick with PHP<\/acronym> 4 for that much longer.<\/p>\n Additionally, if you’re just saving one model (as the result of a form submission) it doesn’t really make sense, nor is it necessary, to have exception handling since CakePHP already takes care of displaying error message for the invalid fields:<\/p>\nSignaling intent<\/h3>\n
if ($this->ModelName->save($this->data))\r\n{\r\n \/\/ Model saved properly, can continue normally here.\r\n ...\r\n}\r\nelse\r\n{\r\n \/\/ Model did not save properly.\r\n}<\/code><\/pre>\n
$html->tagErrorMsg('Post\/title', 'Title is required.')<\/code> in your view to display that error message when a particular field does not validate. This removes a lot of the tedium out of making your forms return proper error messages in situations with bad input.<\/p>\n
Exceptional cases<\/h3>\n
Model::save()<\/code> fails. In this case, having that method throw a proper exception would allow the error to be propagated to the controller nicely and would remove the need to make a call to
Model::invalidFields()<\/code> to get the reason.<\/p>\n
\/\/ Controller:\r\n...\r\nif ($this->Post->save($this->data))\r\n{\r\n $this->flash('Your post has been saved.','\/posts');\r\n}\r\n...\r\n\r\n\/\/ View:\r\n...\r\n<?php echo $html->input('Post\/title', array('size' => '40'))?>\r\n<?php echo $html->tagErrorMsg('Post\/title', 'Title is required.') ?>\r\n...<\/code><\/pre>\n