CakePHP passing data from beforeDelete to afterDelete callback

by | Apr 21, 2025 | IT Tips | 0 comments

Old post found from 2019/06/15 at 7:55 pm in Drafts. Published April 2025

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    private $fileTemplateName = [];
/**
    * @param $cascade
    */
   public function beforeDelete($cascade = true)
   {
       $this->fileTemplateName = $this->findById($this->id);
       return true;
 
   }
 
   public function afterDelete()
   {
       
       $filename = $this->fileTemplateName['PrintTemplate']['file_template'];
       $fileToDelete = WWW_ROOT . Configure::read('GLABELS_ROOT') . DS . $filename;
       $file = new File($fileToDelete);
       $file->delete();
   }

This was a work-a-round because despite CakePHP 2 documentation showing use of $this->data to access record data in the above callbacks $this->data was empty in both afterDelete and beforeDelete for me.

So the answer is in beforeDelete use $this->id to find the record and save it in a class property and then from afterDelete use the record array as needed

Versions: CakePHP 2.10.8 & PHP 7.2

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.