Re: error code from delete or beforeDelete?

2012-05-30 Thread Steve Found

On 30/05/12 14:54, AD7six wrote:


a model test case can exercise all the different scenarios in one hit


A test method should test one thing only, writing multiple scenarios 
in one test method is in and of itself a bad habit (of which I'm 
guilty of doing in the past). Using exceptions or not does not IME 
make testing something any harder or not - you either test that an 
expected exception has been thrown or you check some return variable - 
the amount of work is the same either way.


Totally agree, bad wording on my part. What I meant by one hit was one 
test case rather than a test method. The code being discussed ( a 
controller calling a method in a model and handling exceptions ) implied 
that it would be tested in 2 places. Once for the model test case in the 
raising of the correct exception and once in the controller for handling 
of the exception.


I get the feeling you're voicing an objection to how phpunit handles 
exceptions with annotations rather than exceptions per-se, and you 
could if you wish just wrap your tests in try/catch blocks and test 
the type of exception thrown anyway.
Agreed. But it's really a criticism of how PHP currently handles 
exceptions rather than phpunit. Implementing a throws clause to a 
function would greatly enhance things if it became a syntax error to 
either not catch exceptions or filter them through... for example


public function a() throws AException { throw new AException(); }

// Declared correctly as a() throws an AException which is passed up the 
chain

public function b() throws AException, BException {
a();
throw new BException();
}

// Declared correctly as the AException does not get passed up but B and 
C do.

public function c() throws BException, CException {
try{
b();
}
catch( AException $a ){
throw new CException();
}
}

// Syntax error since BException is not handled and not declared.
public function d() throws CException {
c();
}

At the moment, there is no way of knowing what possible exceptions can 
be thrown by any given function. Calling Model::delete(); could generate 
a whole list of them and without knowing the core source code very well, 
you can not tell from the function prototype. This makes it hard to 
write good tests IMHO and I would guess could also be a problem for 
tests when Exceptions are added or removed from the core in different 
Cake versions.




Here's IMO a better example of when exceptions are appropraite:

try {
$result = $this->Foo->a();
} catch (Exception $e) {  // It's an exception. *something* is wrong, 
it's not necessarily important what - simply handle it gracefully and 
inform the user.

$this->setFlash($e->message);
$this->redirect($this->referer());
}

/**
 * Simulate a complex call to something. all functions in the same 
class for brevity

 */
Class Foo {
function a() {
throw new AException("Some A exception");
...
return $this->b();
}
function b() {
throw new BException("Some B exception");
...
return $this->c();
}
function c() {
throw new CException("Some C exception");
...
return $this->d();
}
function d() {
throw new DException("Some D exception");
...
return $this->e();
}
function e() {
throw new DException("Some D exception");
...
return $result;
}
}

Instead of:

$result = $this->Foo->a();
if (!$result) {
$this->setFlash("Sorry. somewhere an error occurred");
$this->redirect($this->referer());
}

/**
 * Simulate a complex call to something. all functions in the same 
class for brevity

 */
Class Foo {
function a() {
if ("Some A exception") {
return false;
}
...
return $this->b();
}
function b() {
if ("Some B exception") {
return false;
}
...
return $this->c();
}
function c() {
if ("Some C exception") {
return false;
}
...
return $this->d();
}
function d() {
if ("Some D exception") {
return false;
}
...
return $this->e();
}
function e() {
if ("Some E exception") {
return false;
}
...
return $result;
}
}

With the latter - you either need to come up with a new return 
variable format, stash an error message somewhere and retrieve it 
later - or simply accept that you are blind to know where in the 
nested call things stopped and returned false. It's also not easily 
possible to distinguish between a "valid" false return value and some 
unexpected case. With an exception, you can know as much or as little 
as you want about what happened by checking the type of exception 
and/or the message.


Totally agree again, I am in no way saying that exceptions are not 
necessary or should not be used, I was really saying

Re: error code from delete or beforeDelete?

2012-05-30 Thread stork


>  In your example the Category model delete function should not be called 
> unless that category is empty
>

Exactly. There should not be any "delete this category" links rendered in 
views, if category is not "empty", or - they have to be "disabled" - this 
is work for views/helpers.
But:
- this is not enough protection against malicious users or browser history 
or whatever
- controllers should be slim
- model can be used from other parts of an application too
- nobody wants to copy/paste code from controller to console shell/task or 
another associated model, when it comes to situation "you shouldn't want 
this model to do this"

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-30 Thread AD7six


On Wednesday, 30 May 2012 15:00:08 UTC+2, Ratty wrote:
>
> It's far easier to test since all the logic is in the model and a model 
> test case can exercise all the different scenarios in one hit. 
>
> The test with exceptions would need to be something like... 
>
> function testCategoryNotEmptyDeletion { 
>  ... setup illegal data to throw the right exception but none of the 
> others ... 
>  try { 
> $this->Category->delete(); 
> $this->fail('Exception expected'); 
>  } 
>  catch(  CategoryNotEmptyException $cnee ) { 
>  // Pass, This one was expected 
>  } 
>  catch( Exception $e ) { 
>  $this->fail('Unexpected Exception'); 
>  } 
> } 
>
> for each exception that can be thrown making the way they are thrown and 
> the order they are thrown in by beforeDelete() very important. 
>
> say, for example the model had 
>
> CategoryModel::beforeDelete() { 
>  delete or move some related data to allow category deletion 
>  parent::beforeDelete(); 
> } 
>
> MyCategoryModel::beforeDelete() { 
>  ... detect error condition ... 
>  throw new ErrorConditionException('Error detected'); 
>  parent::beforeDelete(); 
> } 
>
> this could be coded as : 
>
> CategoryModel::beforeDelete() { 
>  delete or move some related data to allow category deletion 
>  parent::beforeDelete(); 
> } 
>
> MyCategoryModel::beforeDelete() { 
>  parent::beforeDelete(); 
>  ... detect error condition ... 
>  throw new ErrorConditionException('Error detected'); 
> } 
>
> Which would still pass the test as the exception can be thrown, but the 
> parent has already modified some data. 
>
>
> I totally agree that you could end up checking data twice, but only if 
> you do all the same checks in beforeDelete() as you do in isDeletable(). 
>
> There are some scenarios that should only be possible from the UI like 
> trying to delete a category while it still has items in it. Categories 
> deleted by code would need to remove the items from the category before 
> deleting it and should have a test case to ensure that this happens. 
> beforeDelete() would then have no need to check that condition since it 
> would have been checked by isDeletable(). 
>
>
> I have inherited some code that basically used Exceptions as a form of 
> flow control


I agree that using exceptions for flow control (as gotos) is an awful habit 
that deserves... punishment, I wouldn't say that justifies not using 
exceptions where appropriate - I don't think the examples you're giving 
are analogous.

a model test case can exercise all the different scenarios in one hit
>
>
A test method should test one thing only, writing multiple scenarios in one 
test method is in and of itself a bad habit (of which I'm guilty of doing 
in the past). Using exceptions or not does not IME make testing something 
any harder or not - you either test that an expected exception has been 
thrown or you check some return variable - the amount of work is the same 
either way. 

I get the feeling you're voicing an objection to how phpunit handles 
exceptions with annotations rather than exceptions per-se, and you could if 
you wish just wrap your tests in try/catch blocks and test the type of 
exception thrown anyway.

Here's IMO a better example of when exceptions are appropraite:

try {
$result = $this->Foo->a();
} catch (Exception $e) {  // It's an exception. *something* is wrong, it's 
not necessarily important what - simply handle it gracefully and inform the 
user.
$this->setFlash($e->message);
$this->redirect($this->referer());
}

/**
 * Simulate a complex call to something. all functions in the same class 
for brevity
 */
Class Foo {
function a() {
throw new AException("Some A exception");
...
return $this->b();
}
function b() {
throw new BException("Some B exception");
...
return $this->c();
}
function c() {
throw new CException("Some C exception");
...
return $this->d();
}
function d() {
throw new DException("Some D exception");
...
return $this->e();
}
function e() {
throw new DException("Some D exception");
...
return $result;
}
}

Instead of:

$result = $this->Foo->a();
if (!$result) {
$this->setFlash("Sorry. somewhere an error occurred");
$this->redirect($this->referer());
}

/**
 * Simulate a complex call to something. all functions in the same class 
for brevity
 */
Class Foo {
function a() {
if ("Some A exception") {
return false;
}
...
return $this->b();
}
function b() {
if ("Some B exception") {
return false;
}
...
return $this->c();
}
function c() {
if ("Some C exception") {
return false;
}
...
return $this->d();
}
function d() {
if ("Some 

Re: error code from delete or beforeDelete?

2012-05-30 Thread Steve Found
It's far easier to test since all the logic is in the model and a model 
test case can exercise all the different scenarios in one hit.


The test with exceptions would need to be something like...

function testCategoryNotEmptyDeletion {
... setup illegal data to throw the right exception but none of the 
others ...

try {
   $this->Category->delete();
   $this->fail('Exception expected');
}
catch(  CategoryNotEmptyException $cnee ) {
// Pass, This one was expected
}
catch( Exception $e ) {
$this->fail('Unexpected Exception');
}
}

for each exception that can be thrown making the way they are thrown and 
the order they are thrown in by beforeDelete() very important.


say, for example the model had

CategoryModel::beforeDelete() {
delete or move some related data to allow category deletion
parent::beforeDelete();
}

MyCategoryModel::beforeDelete() {
... detect error condition ...
throw new ErrorConditionException('Error detected');
parent::beforeDelete();
}

this could be coded as :

CategoryModel::beforeDelete() {
delete or move some related data to allow category deletion
parent::beforeDelete();
}

MyCategoryModel::beforeDelete() {
parent::beforeDelete();
... detect error condition ...
throw new ErrorConditionException('Error detected');
}

Which would still pass the test as the exception can be thrown, but the 
parent has already modified some data.



I totally agree that you could end up checking data twice, but only if 
you do all the same checks in beforeDelete() as you do in isDeletable().


There are some scenarios that should only be possible from the UI like 
trying to delete a category while it still has items in it. Categories 
deleted by code would need to remove the items from the category before 
deleting it and should have a test case to ensure that this happens. 
beforeDelete() would then have no need to check that condition since it 
would have been checked by isDeletable().



I have inherited some code that basically used Exceptions as a form of 
flow control and believe me, debugging it and understanding it was a 
nightmare. Admittedly languages like Python rely on them for exactly 
that...  but I am still not sure that is a good thing.


This is all just my opinion of course 

Steve (Ratty)



In what way is that FAR easier to test.


Note you've changed:

$this->Category->delete( $id );

into

if ($this->Category->isDeletable($id)) {
$this->Category->delete( $id );
}

Which means you're either checking things twice - or delete is 
nolonger atomic.


AD
--
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+unsubscr...@googlegroups.com For more options, visit this 
group at http://groups.google.com/group/cake-php


--
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-30 Thread bs28723
Thanks Steve.  This is more the way I was taught years ago. But since I 
was new to PHP, I was not sure it this is new with these OOP.

What you describe makes a lot of sense to me.  Check for all the errors 
conditions that you can up front, and don't let users do things you 
don't want them to do, to maintain data integrity.  The throw is a 
graceful way of catchings things, that should not happen, rather that 
the user just getting a PHP error or a 500 page.

Thanks for helping me make sure I am following best practices.

Bill

On 5/30/2012 7:01 AM, Steve-2 [via CakePHP] wrote:
> Thanks for this thread actually Bill, it has given me a lot of pause 
> for thought...
>
> 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());
>>>
>&g

Re: error code from delete or beforeDelete?

2012-05-30 Thread AD7six


On Wednesday, 30 May 2012 13:00:47 UTC+2, Ratty wrote:
>
>  Thanks for this thread actually Bill, it has given me a lot of pause for 
> thought...
>
> 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.
>

In what way is that FAR easier to test.

Note you've changed:

$this->Category->delete( $id );

into

if ($this->Category->isDeletable($id)) {
$this->Category->delete( $id );
}

Which means you're either checking things twice - or delete is nolonger 
atomic.

AD

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-30 Thread Steve Found
Thanks for this thread actually Bill, it has given me a lot of pause for 
thought...


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.html 

To start a new topic under CakePHP, email [hidden email] 


To unsubscribe from CakePHP, click here.
NAML 
<http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> 




View this message in context: Re: error code from delete or 
beforeDelete? 
<http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340p5708382.html>
Sent from the CakePHP mailing list archive 

Re: error code from delete or beforeDelete?

2012-05-30 Thread Steve Found
My personal view ( and I have been a software engineer for 20 years ) is 
that it is not good practise to rely on exceptions to do error checking 
for you. In languages like Java, a method that throws an exception has 
to be declared as doing so via a 'throws' declaration on the method...


public int foo(int param) throws XYZException, ABCException, ...

but in PHP, there is no declaration at all. You have to read the PHP-Doc 
in the hope that it's up to date or examine the source code to see what 
exceptions could be thrown, catch them and handle them.


Exceptions are really like GOTO statements and are intended for the 
situation where the code cannot possibly continue. The usual response to 
them should be to clean up as much as you can in the catch(){} block to 
avoid any data corruption and stop.


Using them to catch errors can have a lot of hidden traps...  for example...

MyModel::beforeDelete() {
if( errorcondition ) {
throw CustomException("Oooops");
}
parent::beforeDelete();
}

MyController::MyFunction() {
...
try {
$this->MyModel->delete($id);
}
catch( CustomException $ce ){
$this->Session->setFlash('You cant do that !!!');
}
}


This may be OK, but what if the models parent also throws an exception ?

AppModel::beforeDelete() {
if( error condition ){
throw CustomException('Bad News');
}
parent::beforeDelete();
}

MyModel::beforeDelete() {
if( errorcondition ) {
throw CustomException("Oooops");
}
parent::beforeDelete();
}

MyController::MyFunction() {
...
try {
$this->MyModel->delete($id);
}
catch( CustomException $ce ){
// OK, something bad has happened but did it happen in MyModel 
or AppModel ?

$this->Session->setFlash('You cant do that !!!');
}
}

This is what I mean about being a GOTO. The throw in the beforeDelete 
classes will jump straight to the first thing that can catch them, in 
this case the controller. If the exception occurs in the AppModel here, 
it will jump straight to the catch block in the controller and it will 
be down to the catch block to try and clean up the mess that could have 
been left behind.


To my mind, prevention is better than cure. If something can happen that 
can corrupt your data, try to remove the possibility of it happening. 
This can be achieved by using exceptions, but you could end up with some 
large catch blocks in your controllers that would be better suited to 
Model functions.


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.html 

To start a new topic under CakePHP, email [hidden email] 


To unsubscribe from CakePHP, click here.
NAML 
<http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nab

Re: error code from delete or beforeDelete?

2012-05-29 Thread stork


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

Learn from the best ones ;-)

http://mark-story.com/posts/view/simplifying-controller-logic-with-exceptions 

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-29 Thread bs28723
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.html
>  
>
> To start a new topic under CakePHP, email 
> ml-node+s1045679n125572...@n5.nabble.com
> To unsubscribe from CakePHP, click here 
> <http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1255722&code=YmlsbC5zdG9sdHpAYm9vc3RlcndlYnNvbHV0aW9ucy5jb218MTI1NTcyMnwtNTU0NTk2MTUy>.
> NAML 
> <http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>  
>


--
View this message in context: 
http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340p5708382.html
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-29 Thread stork

>
> I need to do more reading on Exceptions, but my case seems more like a 
> flash message than throwing an exception.
>

Go back to reading ;-) because it is easier to throw 2 different custom 
exceptions NotEmptyCategoryException (has some products) and 
NotLeafCategoryException (has some subcategories) from beforeDelete() and 
then either react differently for them or (in controller):

try {
...
} catch (CakeException $e) {
   $this->Session->setFlash($e->getMessage());
}

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-29 Thread Steve Found

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-29 Thread stork
...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());

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-29 Thread stork


> I need to do more reading on Exceptions, but my case seems more like a 
> flash message than throwing an exception.
>

If you have your own custom exception NotEmptyCategoryException, you can 
throw it from beforeDelete() and catch it in controller:

try {
$this->Category->delete($id);
} catch (NotEmptyCategoryException $e) {
$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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-28 Thread bs28723

  

  
  
I need to do more reading on Exceptions, but my case seems more like
a flash message than throwing an exception. 

Maybe I need to do more checking in the controller, rather than
using beforeDelete. 

This way, upon entry into delete method, I could check "count"  and
if =1 then set a flash message and redirect and never display the
form to delete. 

Make sense? 

On 5/28/2012 3:49 PM, stork [via CakePHP] wrote:
 Throw OutOfBoundsException somewhere and catch it
  from outside, or create your own exception
http://book.cakephp.org/2.0/en/development/exceptions.html#creating-your-own-application-exceptions
 
  
  Also, read better beforeDelete() callback for your example: 
  
  public function beforeDelete($cascade = true) { 
      if 
($this->Product->hasAny(array('product_category_id'
  => $this->id))) { 
      return false; 
      } 
      return parent::beforeDelete($cascade); 
  } 
  
  -- 
  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-tp5708340p5708348.html
 
  
  
To start a new topic under CakePHP, email
ml-node+s1045679n125572...@n5.nabble.com  
To unsubscribe from CakePHP, click
  here . 
NAML  

  



--
View this message in context: 
http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340p5708358.html
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-28 Thread stork
Throw OutOfBoundsException somewhere and catch it from outside, or create 
your own exception 
http://book.cakephp.org/2.0/en/development/exceptions.html#creating-your-own-application-exceptions

Also, read better beforeDelete() callback for your example:

public function beforeDelete($cascade = true) {
if ($this->Product->hasAny(array('product_category_id' => $this->id))) {
return false;
}
return parent::beforeDelete($cascade);
}

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: error code from delete or beforeDelete?

2012-05-28 Thread Steve Found
beforeDelete() in a model is, as I understand it, intended to do a 
sanity check of the database to ensure that it is safe to do the 
deletion. In the example, 'product_category_id' is a foreign key in the 
products table which indexes the category you are about to delete. This 
would cause a database error at best or corruption of the database at 
worst depending on how foreign keys are being handled, if at all.


You should not really be using it to detect errors for you. You could, 
for example, have the body of that function in a function called 
'isReferenced()' and beforeDelete() could just be a call to that. In 
your controller, you could then call 'Category->isReferenced()' and 
either disable the delete option or display a message like "Category 
contains products, please remove them first".




On 28/05/12 16:48, bs28723 wrote:
This has got to be a newbie question. I have searched, but can't seem 
to find an answer.


Is there a way to get an error code for why the delete or beforeDelete 
failed and have this passed back to the Controller instead of just false?


using the example from the documentation

 1. |function beforeDelete()|
 2. |{|
 3. |$count = $this->Product->find("count", array(|
 4. |"conditions" => array("product_category_id" => $this->id)|
 5. |));|
 6. |if ($count == 0) {|
 7. |return true;|
 8. |} else {|
 9. |return false;|
10. |}|
11. |}|

in this example if there are products left in the category ($count >0) 
it will return false.  But how does the controller know that this was 
reason?  What if before delete checked for products AND 
subcategories?  The controller/view would want to send a message to 
user as to why the delete failed.


What am I missing?

Thanks,
bill



----------------
View this message in context: error code from delete or beforeDelete? 
<http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340.html>
Sent from the CakePHP mailing list archive 
<http://cakephp.1045679.n5.nabble.com/> 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+unsubscr...@googlegroups.com For more options, visit this 
group at http://groups.google.com/group/cake-php


--
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


error code from delete or beforeDelete?

2012-05-28 Thread bs28723

  


  
  
This has got to be a newbie question. I have searched, but can't
seem to find an answer. 

Is there a way to get an error code for why the delete or
beforeDelete failed and have this passed back to the Controller
instead of just false? 

using the example from the documentation 

  function beforeDelete()
  {
   $count
  = $this->Product->find("count", array(
   "conditions" =>
array("product_category_id" => $this->id)
   ));
   if ($count == 0) {
   return true;
   } else {
   return false;
   }
  }

in this example if there are products left in the category
  ($count >0) it will return false.  But how does the controller
  know that this was reason?  What if before delete checked for
  products AND subcategories?  The controller/view would want to
  send a message to user as to why the delete failed. 

What am I missing? 

Thanks, 
  bill 
  


  



--
View this message in context: 
http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340.html
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php