Monday, May 28, 2012

Re: error code from delete or beforeDelete?

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