We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



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



2.5k

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.



2.5k

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!