Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Check if delete action works

Why this doesn't work???

if ($entity->delete() == false) {
// never work. I have a PDOException in this case
if (!$entity->delete()) {
// never work. I have a PDOException in this case

how to intercept these errors in my app???

thanks

php 5.5

phalcon 1.3.2



1.3k
Accepted
answer

What is the message of the exception?

You can wrap this into a try catch block like so to catch the error:

try {
    if (!$entity->delete()) {
        // actions here
    }

} catch (PDOException $e){
    echo get_class($e), ": ", $e->getMessage(), "\n";
    echo " File=", $e->getFile(), "\n";
    echo " Line=", $e->getLine(), "\n";
    echo $e->getTraceAsString();
}
edited Sep '14

right, works! Thanks!

but, why the phalcon documentation says to do the first form?

this part

if (!$entity->delete()) {

is right, because the registry is not deleted, so why not return the expected result?



1.3k

What error message are you getting when you attempt to delete, I have the forementioned working in my applicatin by just calling it like so.

$model->delete();

Im assuming you are getting an exception from the DB perhaps, relationship integrtiy perhaps?

Yes, I am getting an exception error related to relationship integrity.

so I need to catch this error to display a message to the user.

how to proceed then?

sory for my english.



1.3k

If you are getting the relationship integrity exception then you will have to wrap the delete around the try catch block.

Do you believe you should be able to delete that record and not receiving the relationship integrity issue?

edited Sep '14

changing the database (ON DELETE CASCADE), yes.

but I have other errors not handled, so I'd better do as you said

thanks!