Jimothey
Jimothey

Reputation: 2434

Set a flash message from beforeDelete() in Model - CakePHP

I'm trying to check if a recording that a user is trying to delete has any attached records (in this case a user with attached expense claims). I can do that fine using the beforeDelete() model function. However I want to pass back an flash message if records are found and the delete is not allowed but I just get the following error:

Fatal error: Call to a member function setFlash() on a non-object in...

Heres my code:

public function beforeDelete($cascade  = false) {

    $count = $this->ExpenseClaim->find("count", array(
        'conditions' => array('ExpenseClaim.user_id' => $this->id)
    ));

    if ($count == 0) {
        return true;
    } else {
        $this->Session->setFlash('User cannot be deleted as they have ' . $count . 'number of expenses claims already in the system');
        return false;
    }

}

Could someone point me in the right direction?

Thanks in advance

Upvotes: 1

Views: 2287

Answers (3)

user1970565
user1970565

Reputation: 44

@motsmanish answer aligns with best practices because it puts the code to prevent deletion where it belongs - in the Model, and in CakePHP, this belongs in the beforeDelete() method. To augment this, you can then reference the Session flash message in the method in your controller from which you are attempting the delete:

//e.g. in UserController.php, within public function delete($id){...}

if(!$this->User->delete($id)) {
    if(!$this->Session->check('Message.flash')) {
        $this->Session->setFlash(__('The User could not be deleted'));
    }
} else {
//success stuff
}

The point of this is to allow the flash message set in beforeDelete() to persist if it exists, but to provide a different message if the delete has failed for some other reason.

Upvotes: -1

MotsManish
MotsManish

Reputation: 485

Set a flash message from beforeDelete() in Model

public function beforeDelete($cascade  = false) {
//count query
$count = $this->ExpenseClaim->find("count", array(
    'conditions' => array('ExpenseClaim.user_id' => $this->id)
));
//count checking
if ($count == 0) {
    return true;
} else {
    //custom flash message 
    SessionComponent::setFlash('User cannot be deleted as they have ' . $count . 'number of expenses claims already in the system');
    return false;
}

}

Upvotes: 3

Alvaro
Alvaro

Reputation: 41595

What you should do it checking at the controller that the user could not be deleted and set the flash message from there.

As you are returning false in the User model if the user can not be deleted, it is simple:

if(!$this->User->delete($id){
     $this->Session->setFlash('User cannot be deleted');
}else{
    //....
}

If you want to give the user more detail about the reason I would recommend you to create a function in the User model to check for the number of claims of the user to delete.

This way, you could to something like this in the controller:

if($count = $this->User->getClaims($id)){
    $this->Session->setFlash('User cannot be deleted as they have ' . $count . 'number of expenses claims already in the system');
    $this->redirect(array('controller' => 'User', 'action' => 'index'));

}

Having this in your User model:

public function getClaims($id){
    return $this->ExpenseClaim->find("count", array(
    'conditions' => array('ExpenseClaim.user_id' => $this->id)
));
}

Although it would be better to call directly to the ExpenseClaim model.

Upvotes: 4

Related Questions