Your Location is: Home > Cakephp

Validate field in beforedelete

From: Papua-Neuguinea View: 1930 DuaneW 

Question

I want to check the value of a field in the Model beforedelete.

The beforedelete function has the record id accessed by $this->id but can I access another field?

I have a field called "state" and if the state is not 0 I want to return false (delete not allowed).

I don't think the record is retrieved in beforedelete so how can I retrieve the value so I can check it?

Best answer

Just use find in your beforedelete :)

public function beforeDelete() {
  $record = $this->findById($this->id);

  if(empty($record) {
      // handle case where $this->id doesn't correspond to any record in your db.
  } else {
      if($record['state'] !== 0) {
          return false;
      }
  }
}