Giter Club home page Giter Club logo

cakephp-csvview's Introduction

CI Coverage Status Total Downloads Latest Stable Version Software License

CsvView Plugin

Quickly enable CSV output of your model data.

This branch is for CakePHP 5.x. For details see version map.

Background

I needed to quickly export CSVs of stuff in the database. Using a view class to iterate manually would be a chore to replicate for each export method, so I figured it would be much easier to do this with a custom view class, like JsonView or XmlView.

Installation

composer require friendsofcake/cakephp-csvview

Enable plugin

Load the plugin by running command

bin/cake plugin load CsvView

Usage

To export a flat array as a CSV, one could write the following code:

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOption('serialize', 'data');
}

All variables that are to be included in the csv must be specified in the serialize view option, exactly how JsonView or XmlView work.

It is possible to have multiple variables in the csv output:

public function export()
{
    $data = [['a', 'b', 'c']];
    $data_two = [[1, 2, 3]];
    $data_three = [['you', 'and', 'me']];

    $serialize = ['data', 'data_two', 'data_three'];

    $this->set(compact('data', 'data_two', 'data_three'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOption('serialize', $serialize);
}

If you want headers or footers in your CSV output, you can specify either a header or footer view option. Both are completely optional:

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $header = ['Column 1', 'Column 2', 'Column 3'];
    $footer = ['Totals', '400', '$3000'];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOptions([
            'serialize' => 'data',
            'header' => $header,
            'footer' => $footer,
        ]);
}

You can also specify the delimiter, end of line, newline, escape characters and byte order mark (BOM) sequence using delimiter, eol, newline, enclosure and bom respectively:

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOptions([
            'serialize' => 'data',
            'delimiter' => chr(9),
            'enclosure' => '"',
            'newline' => '\r\n',
            'eol' => '~',
            'bom' => true,
        ]);
}

The defaults for these options are:

  • delimiter: ,
  • enclosure: "
  • newline: \n
  • eol: \n
  • bom: false
  • setSeparator: false

The eol option is the one used to generate newlines in the output. newline, however, is the character that should replace the newline characters in the actual data. It is recommended to use the string representation of the newline character to avoid rendering invalid output.

Some reader software incorrectly renders UTF-8 encoded files which do not contain byte order mark (BOM) byte sequence. The bom option is the one used to add byte order mark (BOM) byte sequence beginning of the generated CSV output stream. See Wikipedia article about byte order mark for more information.

The setSeparator option can be used to set the separator explicitly in the first line of the CSV. Some readers need this in order to display the CSV correctly.

If you have complex model data, you can use the extract view option to specify the individual Hash::extract()-compatible paths or a callable for each record:

public function export()
{
    $posts = $this->Posts->find();
    $header = ['Post ID', 'Title', 'Created'];
    $extract = [
        'id',
        function (array $row) {
            return $row['title'];
        },
        'created'
    ];

    $this->set(compact('posts'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOptions([
            'serialize' => 'posts',
            'header' => $header,
            'extract' => $extract,
        ]);
}

If your model data contains some null values or missing keys, you can use the null option, just like you'd use delimiter, eol, and enclosure, to set how null values should be displayed in the CSV.

null defaults to ''.

Automatic view class switching

You can use the controller's content negotiation feature to automatically have the CsvView class switched in as follows.

Enable csv extension parsing using $routes->addExtensions(['csv']) within required scope in your app's routes.php.

// PostsController.php

// Add the CsvView class for content type negotiation
public function initialize(): void
{
    parent::initialize();

    $this->addViewClasses(['csv' => 'CsvView.Csv']);
}

// Controller action
public function index()
{
    $posts = $this->Posts->find();
    $this->set(compact('posts'));

    if ($this->request->is('csv')) {
        $serialize = 'posts';
        $header = array('Post ID', 'Title', 'Created');
        $extract = array('id', 'title', 'created');

        $this->viewBuilder()->setOptions(compact('serialize', 'header', 'extract'));
    }
}

With the above controller you can now access /posts.csv or use Accept header text/csv to get the data as csv and use /posts to get normal HTML page.

For really complex CSVs, you can also use your own view files. To do so, either leave serialize unspecified or set it to null. The view files will be located in the csv subdirectory of your current controller:

// View used will be in templates/Posts/csv/export.php
public function export()
{
    $posts = $this->Posts->find();
    $this->set(compact('posts'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOption('serialize', null);
}

Setting a different encoding to the file

If you need to have a different encoding in you csv file you have to set the encoding of your data you are passing to the view and also set the encoding you want for the csv file. This can be done by using dataEncoding and csvEncoding:

The defaults are:

  • dataEncoding: UTF-8
  • csvEncoding: UTF-8

** Only if those two variable are different your data will be converted to another encoding.

CsvView uses the iconv extension by default to encode your data. You can change the php extension used to encode your data by setting the transcodingExtension option:

$this->viewBuilder()->setOption('transcodingExtension', 'mbstring');

The currently supported encoding extensions are as follows:

  • iconv
  • mbstring

Setting the downloaded file name

By default, the downloaded file will be named after the last segment of the URL used to generate it. Eg: example.com/my-controller/my-action would download my-action.csv, while example.com/my-controller/my-action/first-param would download first-param.csv.

In IE you are required to set the filename, otherwise it will download as a text file.

To set a custom file name, use the Response::withDownload() method. The following snippet can be used to change the downloaded file from export.csv to my-file.csv:

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $this->setResponse($this->getResponse()->withDownload('my-file.csv'));
    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('CsvView.Csv')
        ->setOption('serialize', 'data');
}

Using a specific View Builder

In some cases, it is better not to use the current controller's View Builder $this->viewBuilder() as any call to $this->render() will compromise any subsequent rendering.

For example, in the course of your current controller's action, if you need to render some data as CSV in order to simply save it into a file on the server.

Do not forget to add to your controller:

use Cake\View\ViewBuilder;

So you can create a specific View Builder:

// Your data array
$data = [];

// Options
$serialize = 'data';
$delimiter = ',';
$enclosure = '"';
$newline = '\r\n';

// Create the builder
$builder = new ViewBuilder();
$builder
    ->setLayout(false)
    ->setClassName('CsvView.Csv')
    ->setOptions(compact('serialize', 'delimiter', 'enclosure', 'newline'));

// Then the view
$view = $builder->build($data);
$view->set(compact('data'));

// And Save the file
file_put_contents('/full/path/to/file.csv', $view->render());

cakephp-csvview's People

Contributors

admad avatar ajibarra avatar ashikkalavadiya avatar chronon avatar ckeboss avatar dakota avatar daoutis avatar dereuromark avatar didos avatar gaurish avatar gmponos avatar hdogan avatar holywise avatar howardbraham avatar iandenh avatar josegonzalez avatar joshuapaling avatar k1low avatar martonmiklos avatar modicrumb avatar mozillamonks avatar plazareff avatar ravage84 avatar rrd108 avatar sdarmofal avatar skie avatar styks1987 avatar tai-sho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cakephp-csvview's Issues

Export records with HABTM

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)?

Feature Request: Provide a "map" Feature For Extracted Array Values

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.

Out of memory,

Hi,
how make to export big tables?
when export over 30000 records, I receive the error out of memory.

Exporting Fields with NULL value?

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();

}

accessing fields with enableAutoFields

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

1 million records

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?

CSV file is empty when using get method, though Arrray has values

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;

}

Example of a complex csv view file

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

Custom variables

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.

Composer require fails

Both
composer require friendsofcake/cakephp-csvview:~2.0
and
composer require friendsofcake/cakephp-csvview

generate an error.

The errors are the same but the specific error for the latter is:

screen shot 2016-06-09 at 11 47 42 am

PHP 5.6.19
Cake v3.2.5

Empty file

Does not work with
$this->autoRender = false;

support for cakephp 4

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

Need extra line of code in one of the examples

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.

Getting view error on cakePHP 3.2

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

unable to unit test for downloads

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?

CakePHP 3

Do you have plans for CakePHP 3.0?

CSV export in cake 3.6

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.

Store at a path

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?

Outputting large amounts of rows

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)

ignore arrays by default

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

Date fields exported as NULL

Everything works great but, date fields are exported as NULL
Any advise would be really apreciated :D

Here is the table:
table

Here is the exported file, check out fecha_de_ingreso field as NULL:
error

Here is the mysql workbench screenshot of the table:
mysql

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'));
    }

Failed to render CSV format

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]

filename extension doesn't work in chrome browser

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?

Safary issue with generated CSV

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?

NULL default should be empty string

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?

Got encoding issue with arabic characters

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 ?

Problem with separator

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

exported/downloaded files missing .csv extension

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'));
}

Add paginator

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.

Export data from query with related table

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.

Extending the CsvView class and RequestHandler

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.

EventManager::instance()->on('Controller.initialize', function (Event $event) {
$controller = $event->getSubject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv');
}
});

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'); 
}

UTF-8 BOM

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();

Tagged versions

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.

Outputting recursive belongsTo associations

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?

CsvView falls over when presented with models with relations

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.

BOM appears in all rows if _bom is enabled

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.

csvView save only html code

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

finder with associated model

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

Not support CakePHP 2.X?

Hi,

I am current using CakePHP 2.7.7. Does this library supports CakePHP 2.x?

Thanks in advance.

Support virtual fields in $_extract list for CSV generation

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.

Layout and helpers?

It would be especially nice to be able to use this plugin with some of my formatting helpers at my disposal.

var_export does not handle circular references

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'

Missing template in export

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.