2

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.

Nunser
  • 4,512
  • 8
  • 25
  • 37
louis_coetzee
  • 393
  • 7
  • 14

1 Answers1

3

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

Community
  • 1
  • 1
thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • the naming of the variables is irrelevant. it suffices to remove the `&` IMO. – mark Apr 19 '13 at 08:25
  • @mark thanks, I wasn't behind my computer and not sure if it was also requiring the parameter _names_ to match for a compatible signature in strict mode (_personally_ I *hate it* if variable names are inconsistent, especially as `{@inheritdoc}` won't make any sense in your PhpDoc). I'll update my answer – thaJeztah Apr 19 '13 at 08:49
  • You are right about that. It's all about consistency. Just saying :) – mark Apr 19 '13 at 08:54
  • @mark Guess I'm more **strict** than *php-strict* is. I'll file a feature-request to add *php-fanatically-strict* mode :D – thaJeztah Apr 19 '13 at 08:58
  • Hi guys, I removed the & before $Model and that removed the warning. thanks a lot. – louis_coetzee Apr 19 '13 at 11:35