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

≡放荡痞女 提交于 2019-12-22 17:48:03

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/16091549/strict-2048-declaration-of-csvimportbehaviorsetup-should-be-compatible-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!