Skip to content

CakePHP Common Issues

Declaration of beforeSave() should be compatible with...

Note

Applies to CakePHP v2.x.

Problem

You receive the following or similar error message "Declaration of beforeSave() should be compatible with Model::beforeSave( $options = array() )".

Solution

  1. Properly adjust the code.
    <?php
    //before
    public function beforeSave( ) {
    
    //After
    public function beforeSave( $options = array()  ) {
    
  2. Configure the error handler configuration in the core.php file.
    <?php
    //Before
    Configure::write('Error', array(
     'handler' => 'ErrorHandler::handleError',
     'level' => E_ALL & ~E_DEPRECATED,
     'trace' => true
    ));
    
    //After
    Configure::write('Error', array(
     'handler' => 'ErrorHandler::handleError',
     'level' => E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED,
     'trace' => true
    ));
    

Associated Models Not Being Retrieved

Note

Applies to CakePHP v2.x.

Solution

Set the recursive property on the model to the desired depth prior to querying for data. A value of -1 will return only the parent table with no joins.

<?php
$this->User->recursive = 2;