How to do set sessionflash if SQLSTATE[23000] foreign key cakephp -
i have 2 tables:
when try delete product_categories
shows following error:
sqlstate[23000] integrity constraint violation: 1451 cannot delete or update parent row: foreign key constraint fails
i want set session flash if error?
its hard answer without actual code have written here generic answer based on little have shared. more detail found @ cake website @ this link
assuming calling controller
if ($this->productcategory->deleteall(array('conditions' => array('id' => $someid)) { // else - set success flash message $this->session->setflash('something good.', 'default', array(), 'good'); } else { // error occurred $this->session->setflash('something bad.', 'default', array(), 'bad'); }
you can cascade deletes child tables setting additional parameter in delete statement above - found @ same link. if cascade deletes statement below - notice true second parameter
if ($this->productcategory->deleteall(array('conditions' => array('id' => $someid), true) { // else - set success flash message $this->session->setflash('successfully deleted product category', 'default'); } else { // error occurred $this->session->setflash('cannot delete product category. products still using category.', 'default'); }
also suggest not enforce integrity constraints in database - nothing wrong make logic more complex.
Comments
Post a Comment