views:

44

answers:

1

Hello,

I want to test if an object is deleted after a calling to my function executeDelete in order to send to the user an error if the object is still in my database.

if ($logement->isDeleted()) {
  $this->getUser()->setFlash('notice', 'Suppression du logement effectuée');
}
else {
  $this->getUser()->setFlash('error', 'Erreur lors de la suppression du logement');
}

But I have an error :

Unknown method Logement::isDeleted

I don't find how to use this method, and I think it's the problem I have.

+1  A: 

You might have to show us more code... But basically your method does not exist, and you would have to create it.

I assume you're using Doctrine. Assuming that you are deleting the record like so:

$lodgement->delete();

Doesn't the delete method return a boolean to indicate success/failure? So you could simply do the following:

if ($lodgement->delete()) {
    $this->getUser()->setFlash('notice', 'success');
} else {
    $this->getUser()->setFlash('error', 'failure');
}

EDIT

If you wanted to implement a isDeleted() method you could use the postDelete() hook. In your model class:

class Lodgement extends BaseLodgement
{
    // add an 'isDeleted' property
    protected $isDeleted = false;

    // override the postDelete method
    public function postDelete($values)
    {
        $this->isDeleted = true;
    }

    // define your own isDeleted method
    public function isDeleted()
    {
        return $this->isDeleted;    
    }
}

Then you can do this:

$lodgement->delete();
echo $lodgement->isDeleted() ? 'notice' : 'error';
Darragh
Ok, I have shown on a site about Propel methods like isDeleted and isModified, and I thounght it was the same thing to Doctrine.I have also shown something about isDeleted on Jobeet :http://www.symfony-project.org/book/1_0/08-Inside-the-Model-LayerAnd I want to know if there is a method to know if an update have worked properly ?
Corum
Indeed.. there is a isDeleted() method in Propel!To check if a record is modified, you can check the state of the object using the state() method. It returns one of the constants defined like STATE_CLEAN, STATE_DIRTY etc.http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_record.html#STATE_CLEANAs for checking an update, save() doesn't return a value, it just throws an exception on failure, but you could use trySave() which returns a boolean based on the result...Again, there's a also postUpdate() hook if you wanted to implement some of your own logic :)
Darragh