proloser / cakephp-csv Goto Github PK
View Code? Open in Web Editor NEWA component that will import/export data from a csv file into a save-friendly nested model data array format.
License: MIT License
A component that will import/export data from a csv file into a save-friendly nested model data array format.
License: MIT License
How do I install the plug-in?
cakephp 3 plugins in the plugin was downloaded copy.
And I added the following code to initialize method to fooTable.php.
$ options = array (
// Refer to php.net fgetcsv for more information
'length' => 0,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '',
// Generates a Model.field headings row from the csv file
'headers' => true,
// If true, String $ content is the data, not a path to the file
'text' => false,
);
$ this-> addBehavior ('CakePHPCSV.Csv', $ options);
Running comes the following error:
CakePHPCSV.CsvBehavior could not be found.
Make sure your plugin CakePHPCSV is in the / var / www / source / plugins / directory and was loaded.
composer not found CakePHP-CSV
When i am trying to add behavior inside src/Model/Table/ and then try export csv its giving me error not found any behavior. then after that non object of exportCsv()
into file vendor\proloser\cakephp-csv\src\Model\Behavior\CsvBehaviore at row 142 there is var_dump($value);
Is a debug line?
thanks
Hello , can you give us the url to install CakePHP-CSV through composer ?
thanks
I am trying to export data into .csv format, and it gets the below error:
iconv(): Detected an illegal character in input string
In CsvComponent on line 44 i.e. the return iconv()
protected function _encode($str = '') {
return iconv("UTF-8", "WINDOWS-1257", html_entity_decode($str, ENT_COMPAT, 'utf-8'));
}
$this->Csv->export($filePath, $data);
The above line shows error app/View/Heper/CSVHelper class missing. The file is getting created but not auto-downloading
I am using cakephp 2.x
Not working in safari
I can not use the method "import", it always returns "false" this is my code: if($this->request->is('post')){ $content = file_get_contents($this->request->data["csv"]["tmp_name"]); file_put_contents(WWW_ROOT."files".DS."{$this->request->data["csv"]["name"]}", $content); $fields = array( "Model.colum", "Model.colum", "Model.colum", "Model.colum" ); // attempt1 $this->data = $this->Csv->import($this->request->data["csv"]["tmp_name"], $fields); // attempt2 $this->data = $this->Csv->import($content, $fields); // attempt3 $this->data = $this->Csv->import(WWW_ROOT."files".DS."{$this->request->data["csv"]["name"]}", $fields); debug($this->data); echo '<pre>';var_dump($this->data);echo '</pre>'; }
Dear Friends,
After I successfully fetch the data from CSV file, if the CSV file header having space (Ex: Created Date)
, then the array key also [created data]
, so how I can make the array key as [created_date]
even if there is space in the headers of CSV?
Array
(
[0] => Array
(
[created date] => 09-05-2018
[indicator] => I
[material_division_code] => 001
[material_group_code] => 11003
[material_code] => 1500001
[description] => COOLGALE 48 COPPER
[material_type] => RM
[hsn_sac] => 9954
[uom] => KG
[net_weight] => 0
[gross_weight] => 0
[volume] => 100
[volume_unit] => M3
[diamention] => 10 x 12 x 2
[part_no] => 33856
[lot_no] => 50
)
)
If you are uploading a CSV created on a Mac you may have issues with it not detecting line endings correctly.
I fixed this by adding the following to the initialize method in the component:
ini_set("auto_detect_line_endings", true);
I am trying to Export a file at a custom location in a webroot directory, for that purpose I am using $this->Csv->export($path, $data); but this method is returning 'False'. Do I need to create a Csv file in order to export data or the export method itself creates the file and exports data?
Please suggest the steps to rectify this problem. Thanks
Missing Plugin
Cake\Core\Exception\MissingPluginException
Documentation API
Error: The application is trying to load a file from the CakePHPCSV plugin.
Make sure your plugin CakePHPCSV is in the D:\xampp\htdocs\cakephp\plugins\ directory and was loaded.
I think it will be a good enhancement. Consider a situation where a CSV template is used with headers in top few rows (for sake of example, top 1 row) to perform bulk upload on some model. This option will come very handy - I have it in my own work to do CSV import.
Anyways, a simple and effective way of doing it can be something like (there are many possible alternatives):
public $defaults = array(
'length' => 0,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
'headers' => true,
'skiprows' => 1 // number of rows to skip from beginning
);
Note the additional entry skiprows
.
Then in your import()
:
while ($row = fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure'])) {
if($r < $options['skiprows']) { // added this condition
$r++; continue; // don't forget to do r++
}
// for each header field
foreach ($fields as $f => $field) {
...
}
r++;
}
...
}
...
}
Does this code have a license? What is it?
Is there a particular reason why $model is being passed as reference in setup() in CsvBehavior?
I'm using PHP 5.5 and CakePHP 2.5.4 and I would get error like:
Strict Error: Declaration of CsvBehavior::setup() should be compatible with ModelBehavior::setup(Model $model, $config = Array)
unless I remove the reference operator from the setup function.
Reckon you could adjust the ReadMe to mention what versions of CakePHP it's compatible with? It would make it easier to judge suitability at a glance. Thanks.
The namespace name is not ideal IMO.
Please consider the following recommendation in such cases:
CakeCsv\\": "src/",
etc
class UsersController extends AppController {
public $components = array('Csv.Csv' => $options);
( ! ) Parse error: syntax error, unexpected '$options' (T_VARIABLE) in /app/Controller/UsersController.php on line 13
I have spotted a strange "return $data;" line 111 of file /models/behaviours/cvs.php.
It is followed by a fclose, a _trigger, and the another return.
I didn't checked the consequences, but I believe it could cause some trouble.
Solution: remove the line 111.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.