Benjamin Allison
Benjamin Allison

Reputation: 2154

Returning false in beforeDelete callback kills deleteAll

When deleting a record, I'm testing to see if a condition is true or false, and based on the result, procede with the deletion or return from it.

The problem is that "return false" stops deleteAll completely. My understanding was that deleteAll would simply move along to the next record.

Is this a bug, or am I missing something?

Upvotes: 2

Views: 864

Answers (1)

Oldskool
Oldskool

Reputation: 34837

The beforeDelete method allows you to abort a delete operation if something seems off. By having it return false, you abort the action. In this case, the deleteAll action, meaning you abort all the deletes. What you're more likely looking for is deleting in a foreach loop, like:

// $data would be the resultset of the rows you want to delete
foreach($data as $row) {
    $this->Model->delete($row['Model']['id']);
}

That way, the beforeDelete is called for each row individually, allowing "valid" rows to be deleted and "invalid" rows to be kept.

Upvotes: 2

Related Questions