Search code examples
cakephpcakephp-2.3cakedc

Strict (2048): Declaration of CsvImportBehavior::setup() should be compatible with ModelBehavior


I am getting the following error:

Strict (2048): Declaration of CsvImportBehavior::setup() should be compatible 
with ModelBehavior::setup(Model $model, $config = Array) 
[APP\Plugin\Utils\Model\Behavior\CsvImportBehavior.php, line 20]

I followed the tutorial on this site: http://www.pronique.com/blog/enable-csv-import-all-controllers-models-cakephp-2

When I import my CSV file, it gives the following flash message:

Successfully imported 0 records from Book1.csv

I don't understand why its not importing, does it have something to do with the error/warning its giving?

I looked inside the behaviour (CsvImportBehaviour.php at line 20): class CsvImportBehavior extends ModelBehavior {
That does not make sense on line 20, that's just the class declaration, so I moved down on the code and saw the following: public function setup(Model &$Model, $settings = array()) {-- this does seem to me to be according to the standards.


Solution

  • To suppress the errors/warnings, try to:

    • remove the & before $Model (not required as Model is an object and therefore already passed byref)

    Optionally (see comment by @mark):

    • rename $Model to $model (lowercase)

    • rename $settings to $config

    I don't know the reason for not importing records from the CSV, that will require debugging on your side.

    Alternatives

    CakePHP also has a CSV dataSource as part of the datasources plug in.

    Using this, you can create a Model that, in stead of using a database, uses a CSV file as its source. This allows you to, for example, do this;

     $csvData = $this->MyCsvModel->find('all');
    

    Which will return all rows from the CSV file. Importing this into your database will be easy to implement by saving $csvData to another model

    Links:

    https://github.com/cakephp/datasources/tree/2.0 https://github.com/cakephp/datasources/blob/2.0/Model/Datasource/CsvSource.php