Sunday, February 13, 2022

[FIXED] Cakephp 3 Callback method not reached

Issue

I have a problem with the afterDelete callback method. I can't use them.

Inside one of my "Storages" plugin controllers I want to delete a record and after that I want to do some other thinks, but the callback method is not reached. I have checked this with adding a log message inside the afterDelete() callback method.

This is the controller where I removed a record:

namespace Storages\Controller;

class StoragecontainerBlocksController extends AppController {

   public function initialize() {
       parent::initialize();
       $this->loadComponent('RequestHandler');
   }

   public function ajaxDeleteBlockElement() {
       $this->autoRender = false;

       // load model
       $this->loadModel("StoragecontainerBlockElements");

       // get element id
       $elementId = $this->request->data('id');

       $this->request->allowMethod(['post', 'delete']);

       // delete element
       $storagecontainerBlockElement = $this->StoragecontainerBlockElements->get($elementId);
       $this->StoragecontainerBlockElements->delete($storagecontainerBlockElement);
   }
}

This is the (model) table where the afterDelete callback is defined:

  use Cake\Log\Log;
  class StoragecontainerBlockElementsTable extends Table {

   public function afterDelete(Event $event) {
       Log::debug('Got here');
   }
  }

Update:

When I debugged Log::debug($this->StoragecontainerBlockElements); inside the ajaxDeleteBlockElement function I got the following array inside debug.log file:

2017-03-31 07:03:48 Debug: Cake\ORM\Table Object
(
    [registryAlias] => StoragecontainerBlockElements
    [table] => storagecontainer_block_elements
    [alias] => StoragecontainerBlockElements
    [entityClass] => \Cake\ORM\Entity
    [associations] => Array
        (
        )

    [behaviors] => Array
        (
        )

    [defaultConnection] => default
    [connectionName] => default

Update:

namespace Storages\Model\Table;

use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Log\Log;

class StoragecontainerBlockElementsTable extends Table {

    public function initialize(array $config) {
       parent::initialize($config);
    }

    public function afterDelete(Event $event) {
       Log::debug('Got here');
    }
}

Solution

As can be seen in the debugging results, $this->StoragecontainerBlockElements is not what you thought it is, it is a so called "auto-table" or "generic table", that is, an instance of \Cake\ORM\Table instead of a concrete subclass thereof.

Your StoragecontainerBlockElementsTable class/file cannot be found/loaded for some reason, hence the fallback to \Cake\ORM\Table. Might be caused by

  • a typo in the filename, classname, or the namespace
  • or the namespace is missing completely (it's not in your question)
  • or the class lives in a plugin, and you didn't used plugin notation for loading it
  • or the file permissions do not allow reading file
  • or the file is missing (not deployed)
  • ...

See also



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.