Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export data from query with related table #131

Open
nicocins opened this issue Jun 9, 2021 · 4 comments
Open

Export data from query with related table #131

nicocins opened this issue Jun 9, 2021 · 4 comments

Comments

@nicocins
Copy link

nicocins commented Jun 9, 2021

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.

@oresch
Copy link

oresch commented Jul 21, 2021

Hi @nicocins ,

I was faced with the same issue and found the following solution.
Assuming you have Devices and DeviceStatuses tables.
The following did help me:

$data = $this->Devices->find()
                ->select([
                    'Devices.id', 'Devices.name', 'Devices.ip_address',
                    'status' => 'DeviceStatuses.name'
                ])
                ->contain([                   
                    'DeviceStatuses'
                ]);

With 'status' => 'DeviceStatuses.name' I was finally able to use the depending information and export it to csv.

And in the $_extract array I put the following - as already mentioned in the doc:

$_extract = [
                'Devices.id',
                'Devices.name',
                'Devices.ip_address',
                function (array $row) {
                    return $row['name'];
                },

Hope this helps

@nicocins
Copy link
Author

Thanks for your help ! i ll try !

@tgoeminne
Copy link

It used to work in the older version, why is not working in this one? Need to update plugin so can work with associated models again.

@dereuromark
Copy link
Member

We should add a note into the docs here I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants