friendsofcake / cakephp-csvview Goto Github PK
View Code? Open in Web Editor NEWCakePHP: A view class for generating CSV
License: MIT License
CakePHP: A view class for generating CSV
License: MIT License
chrome and FF work nicely, but on safari the content is scrambled somehow:
bplist00��_��WebMainResource������
�_��WebResourceMIMEType_��WebResourceTextEncodingName^WebResourceURL_��WebResourceFrameName_��WebResourceDataXtext/csvUUTF-8_�:https://domain/controller/in-progress.csvPO�On<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"Issue","Group",Title,"Status",Priority,Link,Created,Modified
[payload]
</pre></body></html>\00�\00�\00�\00(\00>\00\\00k\00�\00�\00�\00£\00à\00á\00\00\00\00\00\00��\00\00\00\00\00\00\00
\00\00\00\00\00\00\00\00\00\00\00\00\00\00PS
[payload] is the actual rows of data I removed for simplicity
the other browsers also auto trigger download, this one doesnt.
Is this a known issue to others?
I am using CsvView, but I have to pre-process the data before serialization. Given the amount of code involved, I thought I'd put it in a separate view class rather than using _extract
. However, when I point CSV in the viewClassMap
to my custom view class, the value gets overwritten by this plugin.
cakephp-csvview/config/bootstrap.php
Lines 11 to 16 in 3b105f4
I can make a PR to check whether the config is set before overriding it, would that be reasonable for the project? Something like this.
if ($controller->components()->has('RequestHandler') &&
$controller->RequestHandler->getConfig('viewClassMap.csv') === null) {
$controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv');
}
Since "download" function is depricated from cakephp 3.4 have to use
$this->response->withDownload($file_name);
But it does not set the file name.
The solution for open CSV file with dingbats characters in Microsoft Excel is add special characters before return:
Line 158:
return "\xEF\xBB\xBF" . $this->_serialize();
Hi, I have little problem. CsvView save in csv file only html code, something like that: <!DOCTYPE html>
... and whole page... Help pls. What did I miss?
Thks
hi, when i download file.csv in chrome, the browser ignore file extension and download export.
The file has been created correctly, it's can be open with a text editor.
How can i resolve this problem?
I tried a lot of tricks to solve this issue but I fall, I tried this config:
$_extension = 'mbstring'; $_dataEncoding = 'UTF-8'; $_csvEncoding = 'UTF-16LE';
But it solve accentued chars but not arabic chars, any solution for that ?
It would be great if you can add pagination support for CSV data. It will be a very useful feature as per end user's perspective to see the data in a paginated view like it used to see all the other data on website which comes from database.
Addding this line to my controller
public $components = [
'RequestHandler' => [
'viewClassMap' => ['csv' => 'CsvView.Csv']
]
];
is giving error as
var_export does not handle circular references
Context is
$name = 'RequestHandler'
$config = [
'viewClassMap' => [
'csv' => 'CsvView.Csv'
]
]
$existing = object(Cake\Controller\Component\RequestHandlerComponent) {
'components' => [],
'implementedEvents' => [
'Controller.startup' => 'startup',
'Controller.beforeRender' => 'beforeRender',
'Controller.beforeRedirect' => 'beforeRedirect'
],
'_config' => [
'checkHttpCache' => true,
'viewClassMap' => [
[maximum depth reached]
],
'inputTypeMap' => [
[maximum depth reached]
],
'enableBeforeRedirect' => true
]
}
$msg = 'The "RequestHandler" alias has already been loaded with the following config: '
$hasConfig = true
$existingConfig = [
'checkHttpCache' => true,
'viewClassMap' => [
'json' => 'Json',
'xml' => 'Xml',
'ajax' => 'Ajax'
],
'inputTypeMap' => [
'json' => [
(int) 0 => 'json_decode',
(int) 1 => true
],
'xml' => [
(int) 0 => [
[maximum depth reached]
]
]
],
'enableBeforeRedirect' => true
]
$fail = true
$value = [
'csv' => 'CsvView.Csv'
]
$key = 'viewClassMap'
Hello .
How to add color on header row?
In all examples you are using an in memory array, assuming that I want to generate a CSV from a table that has 1 million registries, is there any way I can do that using this Package but without putting the data into physical memory?
How can I export records associated with HABTM?
When I tried to export with help of csvview, it exports only one value but not all the associated values.
I have research programs containing various departments, I want to export the filtered search results as csv.
$this->loadModel('Researchprograms');
$this->loadModel('Departments');
$researchprograms = $this->Researchprograms->find('search', $this->Researchprograms->filterParams($this->request->query)) ->contain(['Users', 'Departments']);
foreach ($researchprograms as $researchprogram):
$data = [$researchprogram];
endforeach;
$_serialize = ['data'];
$this->response->download('my_file.csv');
$this->viewBuilder()->className('CsvView.Csv');
$this->set(compact('data','_serialize'));
This code generates csv file but every record has only one departments, but there are many departments for a record.
Let me know how can I export the records with all associated departments (HABTM)?
Dear all,
I have an issue when I export data with csvview.
I want to export data from two associated table :
$this->response = $this->response->withDownload(‘Inventaire.csv’);
$this->loadModel(‘Details’);
$details = $this->Details->find(‘all’)->contain([‘Scans’])
->where([‘Scans.idInventory’ => $idInventory]);
$details->select([‘Scans.nb_elements’, ‘Details.ean’, ‘Details.qty’]);
$this->set(compact(‘details’));
$this->viewBuilder()
->setClassName(‘CsvView.Csv’)
->setOptions([
‘serialize’ => ‘details’,
‘delimiter’ => ‘;’,
‘enclosure’ => ‘’,
‘bom’ => true
]);
When I export data from only one table (Details) it works, but when I add “Scans” table I got this message:
Notice (8): Array to string conversion [ ROOT\vendor\friendsofcake\cakephp-csvview\src\View\CsvView.php , line 333 ]
It’s like he s trying to display an array instead of my field Scans.nb_elements.
I check my variable $details and everything is ok.
Could you help me about this ?
Thanks in advance.
Hi,
Really great plugin. I need some help. I have a field in database named 'status' which stores values for '1' for Active and '0' for Inactive . I have stored these in a variable. $boolean_values_status = array(1 => "Active", "0" => "Inactive");
So when i download the data, status comes as either 1 or 0. How can i make it print as 'Active' for 1 and 'Inactive' for 0.
when i use find method it works fine.
public function exportdependants($id){
$dependants = $this->Dependants->get($id);
$_header = ['Full Name','Relationship','Date Of Birth','Dependant Until','Gender'];
$_extract = array('full_name','relationship','date_of_birth','dependant_until','gender');
$this->response->download('Dependants.csv');
$_serialize = ['dependants'];
$this->set(compact('dependants','_serialize','_header','_extract'));
$this->viewBuilder()->className('CsvView.Csv');
return;
}
Hi,
I thought going the easy way using enableAutoFields(), but the associated fields are empty.
is there a good practice how to access associated fields instead of defining each single field in the extract ?
thx
BOM should appear in only the first row but it appears in all rows if _bom
is enabled.
I notice this problem in version 3.3.1.
I have fixed it and added test methods.
Does not work with
$this->autoRender = false;
I seem to be having an issue exporting to CSV if one of the columns have a NULL value the whole file comes though blank, once I remove that column from the export it works as needed.
Is this a bug or something I am doing incorrectly?
MY EXPORT FUNCTION CODE:
public function export() {
$cond = array();
if(isset($this->request['named']['dept']))
{
$cond[] = array('Item.department_code' => $this->request['named']['dept']);
}
if(isset($this->request['named']['item']))
{
$cond[] = array('Item.item_number' => $this->request['named']['item']);
}
$items = $this->Item->find('all', array('conditions' => $cond));
$_serialize = 'items';
$_header = array('Submitted By', 'Item', 'Department', 'Current Price', 'Suggested Price', 'Status', 'Date Submitted', 'Item id');
$_extract = array('Feedback.contact_name', 'Item.item_description', 'Item.department_description', 'Item.current_price', 'Item.suggested_price', 'ItemStatus.name', 'Item.created', 'Item.id');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('items', '_serialize', '_header', '_extract'));
$this->response->download('exported_pricing_feedback.csv');
$this->response->send();
}
Hi Jose,
I've been having issues trying to output recursive belongsTo associations using your plugin.
In the query below all the data pulled from the database is available except for items in the 'User' array
$results = $this->Payment->find('all', array(
'contain' => array(
'Invoice'=>
array('Client'=>
array('User')
),
)
));
Is this something that is supported?
I have my application already running with AuthUser component and after installation of cakephp-csvview and copying the first example. I am getting this error.
Do I have to create a layout and view to handle the csv format like it is for pdf or what could be causing this error?
Error: Fatal Error (256): [Cake\View\Exception\MissingHelperException] Helper class AuthUserHelper could not be found.
#0 C:\wamp\www...\vendor\cakephp\cakephp\src\Core\ObjectRegistry.php(91): Cake\View\HelperRegistry->_throwMissingClassError('AuthUser', NULL)
#1 C:\wamp\www...\vendor\cakephp\cakephp\src\View\HelperRegistry.php(67): Cake\Core\ObjectRegistry->load('AuthUser')
#2 C:\wamp\www...\vendor\cakephp\cakephp\src\View\View.php(745): Cake\View\HelperRegistry->__isset('AuthUser')
#3 C:\wamp\www...\src\Template\Element\top_bar.ctp(53): Cake\View\View->__get('AuthUser')
#4 C:\wamp\www...\vendor\cakephp\cakephp\src\View\View.php(828): include('C:\wamp\www\rig...')
#5 C:\wamp\www...\vendor\cakephp\cakephp\src\View\View.php(788): Cake\View\View->_evaluate('C:\wamp\www\rig...', Array)
#6 C:\wamp\www...\vendor\cakephp\cakephp\src\View\View.php(1155): Cake\View\View->_render('C:\wamp\www\rig...', Array)
#7 C:\wamp\www...\vendor\cakephp\cakephp\src\View\View.p in [C:\wamp\www...\vendor\cakephp\cakephp\src\Error\ErrorHandler.php, line 157]
I have an implementation of your library that works in the browser, but when I run the unit test below
public function testReportsExportAuthenticated(){
// Set session data
$this->session([
'Auth' => [
'User' => [
'id' => 1,
'username' => 'admin',
'name' => 'Admin',
'phone_number' => '1234'
]
]
]);
$this->get('/reports/export');
$this->assertResponseOk();
}
the assertResponseOk throws me an error 500. How is it so?
You'll get a lovely screen of "Notice (8): Array to string conversion [ROOT/vendor/friendsofcake/cakephp-csvview/src/View/CsvView.php, line 376]" if you try to use this plugin with a model that's been configured with relationships. The plugin needs to learn to flatten these.
I make liberal use of virtual fields in my application for generating data on the fly, it would be nice to have access to these just like other fields when doing the setup for CSV generation. Currently I'm doing something like this:
$_extract = [
'id',
'full_name' => function ($row) {
return $this->Sps->get($row['id'])->full_name;
},
'user.email',
'birthdate',
];
Which works, and I can live with it, but it would be nice to be less clunky. If this is already supported in some way, it doesn't seem to be documented.
Hi,
could you please add support for cake php 4.x ? Composer will refuse to load this repository.
Problem 1
- Installation request for friendsofcake/cakephp-csvview ^3.4 -> satisfiable by friendsofcake/cakephp-csvview[3.4.0].
- friendsofcake/cakephp-csvview 3.4.0 requires cakephp/cakephp ^3.5.5 -> satisfiable by cakephp/cakephp[3.5.10, 3.5.11, 3.5.12, 3.5.13, 3.5.14, 3.5.15, 3.5.16, 3.5.17, 3.5.18, 3.5.5, 3.5.6, 3.5.7, 3.5.8, 3.5.9, 3.6.0, 3.6.0-RC1, 3.6.0-RC2, 3.6.0-beta1, 3.6.0-beta2, 3.6.0-beta3, 3.6.1, 3.6.10, 3.6.11, 3.6.12, 3.6.13, 3.6.14, 3.6.15, 3.6.2, 3.6.3, 3.6.4, 3.6.5, 3.6.6, 3.6.7, 3.6.8, 3.6.9, 3.7.0, 3.7.0-RC1, 3.7.0-RC2, 3.7.0-RC3, 3.7.0-beta1, 3.7.1, 3.7.2, 3.7.3, 3.7.4, 3.7.5, 3.7.6, 3.7.7, 3.7.8, 3.7.9, 3.8.0, 3.8.0-RC1, 3.8.0-RC2, 3.8.0-RC3, 3.8.0-beta1, 3.8.1, 3.8.10, 3.8.11, 3.8.12, 3.8.13, 3.8.2, 3.8.3, 3.8.4, 3.8.5, 3.8.6, 3.8.7, 3.8.8, 3.8.9, 3.9.0, 3.9.0-RC1, 3.9.0-RC2, 3.9.1, 3.x-dev] but these conflict with your requirements or minimum-stability.
thanks
It would be especially nice to be able to use this plugin with some of my formatting helpers at my disposal.
Hi,
how make to export big tables?
when export over 30000 records, I receive the error out of memory.
I'm using Cake PHP 2.4.5. When I used the code as you have in the snippet above the line
// Access /posts/export.csv to get the data as csv
I found that I also needed the line
$this->viewClass = 'CsvView.Csv';
otherwise I got the error
View file "\app\View\csv\export.ctp" is missing.
Before returning/displaying values extracted from a model-generated array into the output CSV, it would be nice to use a mapping function on each value for pre-processing.
For example: Usually, records with dates are stored in the database as GMT, but we want to convert these dates to the end-user's timezone on the CSV output. The $_extract view variable may be extended in a way to like so: 'Model.field' => 'callableFunction'
, where callableFunction()
will be applied to values extracted from Model.field
and sent as final output.
Hi, i'm trying to display some data from a model that is associated, for example:
$reports = $this->Reports->find('all'); $reports->contain(['Users']);
but the data is not displayed, there is a way i can archieve this?
Thanks
Hi,
The files i exported are missing the .csv extensions. Am i doing something wrong?
This is the codes i use to test the function.
public function export() {
$data = array(
array('a', 'b', 'c'),
array(1, 2, 3),
array('you', 'and', 'me'),
);
$_serialize = 'data';
$this->viewClass = 'CsvView.Csv';
$this->set(compact('data', '_serialize'));
}
Everything works great but, date fields are exported as NULL
Any advise would be really apreciated :D
Here is the exported file, check out fecha_de_ingreso field as NULL:
Here is the mysql workbench screenshot of the table:
Here is my code:
public function export() {
$kids = $this->Kids->find()->where(['user_id'=>$this->Auth->user('id')]);
$_serialize = 'kids';
$_bom = true;
$this->response->download('CONTROL_NIÑOS.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('kids', '_serialize', '_bom'));
}
In the documentation it says "For really complex CSVs, you can also simply use your own view files". Could you kindly provide an example of how such view file would look like? I'm confused as to whether I'd have to print out the headers, separators and escape the data etc. manually
Hi,
I am current using CakePHP 2.7.7. Does this library supports CakePHP 2.x?
Thanks in advance.
I would like to generate really big csv files with large amounts of rows.
public function export()
{
$this->viewBuilder()->className('CsvView.Csv');
$milionsOfRows = $this->SomeBigTable->find()->enableBufferedResults(false)->all();
$this->set('data',$milionsOfRows);
$this->set('_serialize','data');
}
Currently this plugin generates complete csv content before outputting it to the browser.
This can cause slowndowns or running out of memory when dealing with big number of rows.
I think it would be good idea to stream csv output instead (and flush() it every 100 rows)
If I'm just outputting all cols in a table, is it possible to extract col names automatically as the file header?
I was successfully able to export csv but, I want to save this csv file at a path.
Lets Say: If csv file name is test.csv then I want to save this file in webroot/files/csv/.
How can I do this job with this plugin?
Hi there.
First of all, thanks for your job! It's a great addon for cakephp and really helpfull..
Wondering if you can help me out. Basicly, I wanted the csv to work on both apple Numbers and Microsoft Excel. The problem is that apple Numbers use ";" to separate columns, and Microsoft Excel uses "," to separate columns.
Setting the separator, I can make it work on each one of them, but not on both of them.
I got:
$_setSeparator = true;
$_delimiter = ",";
this->set(compact('_setSeparator', '_header', '_extract', '_delimiter', '_bom', '_null', '_dataEncoding', '_csvEncoding'));
Doing anything wrong?
Thanks
Do you have plans for CakePHP 3.0?
I think we need to change the _null default from string 'NULL'
to empty string ''
.
Or is there any specific reason we do it that way?
NULL and empty string in CSV are one and the same, and right now it would conflict with any actual string 'NULL'
which is way worse IMO.
Can we do that in a new minor version?
Is there any plan to setup a semver/tagged/stable version that one could link to from production and be fine with being BC (for instance BC with cakephp 3.1 or 3.2 etc)?
Right now you cannot composer upgrade without being fearful only composer install the fixed version.
any body knows the issue here?
Error: CsvView.Csv could not be found.
Error: Create the class CsvView.Csv below in file: src\View\CsvView.Csv.php
I've put the csvview plugin under myproject/plugins since Im using cakephp 3.2
Hi, I've been playing with this library and I think it might be useful to ignore related data by default when generating csv.
For example if I have an authors table and related posts for each author, I'm getting a warning about array to string conversion. Maybe it would be easier to just ignore these variables...
Then, for "belongs to" relations I would be great to add just required fields from those related models without having to specify all fields from the base model.
I mean - the library would pick up all the fields (except arrays) from the base model (for ex. posts) then I might add fields from authors table (post.author.name) without the hassle of specifying all post's fields.
thanks, dan
Some methods are deprecated in cakephp 3.6 ,
example :
Download to withDownload
Subject to getSubject
Config to getConfig
I have changed all method name but still plugin is not working in cake version 3.6.
Hey josegonzalez,
I tried your plugin out and its very good, thank you for that! I get a problem with the created csv when field values/ content have html and special signs included like ',' or ';' (for example in Product Description).
Do you have and idea for Fixing?
My version of Cakephp is 3.7.9
I am trying to export in a CSV file a content, for example:
`public function export()
{
$data = [
['a', 'b', 'c'],
[1, 2, 3],
['you', 'and', 'me'],
];
$_serialize = 'data';
$this->viewBuilder()->setClassName('CsvView.Csv');
$this->set(compact('data', '_serialize'));
}`
If I access to /controller/export then I have the error message "Missing template export.ctp".
If I access to /controller/export.csv then I have the error message 404.
Thanks.
Would it be possible to get a branch in this repo that requires dev-master? I have an app that uses ~3.1 instead of ~3.0 and right now they're incompatible.
Thanks
cc @josegonzalez
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.