Exceptions are a method of reporting WHAT has gone wrong or is about to go wrong. It is not a method of reporting WHERE it went wrong. In my experience they are primarily used for detecting "This should not happen" conditions. In your example the Category model delete function should not be called unless that category is empty, so an exception could be thrown if that condition is not met. This is a long way away from just calling delete() and see if it works. Remember that an exception can be thrown in beforeDelete(), delete(), afterDelete() in your model, any parent model, or any related model that is affected in HABTM, HasMany, ... relationships and they will all arrive at your catch block. You could end up with code like...
try {
$this->Category->delete( $id );
} catch( NotEmptyCategoryException $nece ) {
....
} catch( CategoryHasChildCategoriesException $ccce ) {
....
} catch( CategoryReferencedInBlogTableException $crbe ) {
....
} catch( ChildCategoryOfThisCategoryNotEmptyException $damn ) {
....
}
This logic, to my mind, would be far better positioned in the model function...
Category::isDeletable($id) {
$result = true;
if( $this->hasItems() ) {
$this->log( 'Cannot delete category containing items.', 'debug' );
$result = false;
}
if( $this->hasChildCategories() ) {
$this->log('Cannot delete category with child categories.', 'debug' );
$result = false;
}
...
return $result;
}
CategoriesController::delete( $id ) {
try {
if( $this->Category->isDeletable($id) ) {
$this->Category->delete($id);
}
}
catch( Exception $e ){
// If isDeletable has done it's job, this should never happen.
$this->log( $e->message(), 'error' );
....
}
}
Not only is this cleaner, but the model is FAR easier to write test cases for.
Steve (Ratty)
On 29/05/12 20:45, bs28723 wrote:
Thanks @stork & @steve-2 for the examples - great stuff!
Can either of you, give me some best practices for exceptions processing? Maybe it is my self-taught PHP & CakePHP experiences but I have not a done a lot with exceptions. Is this efficient? Any good references to do some reading on this?
Thanks,
bill
On 5/29/2012 2:17 PM, Steve-2 [via CakePHP] wrote:On 29/05/12 15:35, stork wrote:
> ...or even better:
>
> try {
> if ($this->Category->delete($id)) {
> $this->Session->setFlash('Category has been deleted');
> $this->redirect(array('action' => 'index'));
> } else {
> $this->Session->setFlash('Unknown error');
> }
> } catch (NotEmptyCategoryException $e) {
> $this->Session->setFlash('This category is not empty');
> }
> $this->redirect($this->referer());
...or even better:
if( $this->Category->isEmpty() ) {
if ($this->Category->delete($id)) {
$this->Session->setFlash('Category has been deleted');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unknown error');
}
} else {
$this->Session->setFlash('This category is not empty');
}
$this->redirect($this->referer());
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
If you reply to this email, your message will be added to the discussion below:http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340p5708378.htmlTo start a new topic under CakePHP, email [hidden email]
To unsubscribe from CakePHP, click here.
NAML
View this message in context: Re: error code from delete or beforeDelete?
Sent from the CakePHP mailing list archive at Nabble.com.
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment