I’m trying to execute some logic before deleting a field. I have some models that are dependent on the model being deleted, and I want to make sure that image files related to those dependent models are also deleted, but I’m a bit confused on how the model callbacks work.
I know that I define the before Delete function in the model class, but How do I access the data in the current model or dependent models being deleted?
function beforeDelete() {
}
I’m just a little confused as how to use these callbacks, and I haven’t seen any great documentation out there.
Edit:
After adding this to the parent model, it seems to always return false.
function beforeDelete() {
if ($this->DependentModel->find('count', array('conditions' => array('DependentModel.parent_id' => $this->id))) == 1){
return true;
} else{
return false;
}
}
Should be obvious what I’m trying to do here. If there is one entry of the dependent model present in the table, return true and continue the deletion. I made sure that there is in fact one table entry that is dependent on the object being deleted. When I execute the delete action it always returns false. What’s going on here?
When using callbacks, you can refer to the API for the class you are extending to check the parameters it accepts. Your implementation should accept, at minimum, the same parameters as the methods you are overriding.
For example,
Model::beforeDeleteis implemented like so:And also,
ModelBehavior::beforeDeleteis implemented like this (ie. when making a behavior):Next, it is useful to know that when saving to a model and passing in the controller’s data (ie.
$this->datain the controller) that data is set to the model (ie.$this->datain the model). [This happens duringModel::save(), currently on line 1225.]In the case of the first example you can access the model using
$thisand in the second example you can access the model using$model(as$thiswould be the behavior in that context). So to get at the data, you want to use$this->dataor$model->data. You can also access that model’s related models using chaining (ie.$this->RelatedModelor$model->RelatedModel).As the docblock comments state,
$cascadeshould let you know if this is a cascading delete that is happening (trueby default) in case your code needs to take different actions when this is or isn’t the case; and your implementation of the method should return false if you want to abort the save operation (otherwise, return true when you are done).There is a Media plugin for CakePHP which implements this exact functionality that can be used as a reference.