1
votes

I've recently updated my MAMP server to PHP 7.1 and build a project with Cakephp 3.x. But I get a strange 500 (Internal Server Error) error when I use the afterSave() callback.

The documentation lifecycle-callbacks describes the following:

public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) {
   // some action 
}

When I use PHP 7.1 it will give's me a 500 error, but if I use PHP 5.6 it will work for me.

Now I've fixed this 500 error on PHP 7.1 by no longer defining the types inside the function. But is this the correct way to do this?

public function afterSave($event, $entity, $options) { 
   // some action 
}

Update:

My error log says:

2017-05-06 13:38:09 Error: [TypeError] Argument 1 passed to Storages\Model\Table\StoragecontainerBlockElementsTable::afterSave() must be an instance of Storages\Model\Table\Event, instance of Cake\Event\Event given, called in /Applications/MAMP/htdocs/safebend-community-data-center/community/vendor/cakephp/cakephp/src/Event/EventManager.php on line 414 Request URL: /storages/blocks/dsdsdsd/ajaxAddElement Referer URL: http://localhost:8888/safebend-community-data-center/community/storages/blocks/dsdsdsd

My Table with namespace:

namespace Storages\Model\Table;

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

class StoragecontainerBlockElementsTable extends Table { 
    public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) { 
        // some action
    }
}

My Controller with Namespace:

namespace Storages\Controller;

use App\Controller\AppController;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;

class StoragecontainerBlocksController extends AppController { }
1
That's most definitely not the correct way, as you're just working around the actual problem. Check your cake/php/server error logs. - ndm
Indeed, I thought so too. I've updated my question with the error log. - CodeWhisperer

1 Answers

2
votes

You haven't import the used names, hence Event will refer to the current namespace, ie the argument will be typed as \Storages\Model\Table\Event instead of the expected \Cake\Event\Event. The same problem exists for the two other arguments.

Import the class names and you should be good:

use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;

Failing to do so should cause an error in any PHP version though (given that the method is being invoked).