Skip to content

Export data from query with related table #131

Closed

Description

@nicocins

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.

Activity

  1. oresch commented on Jul 21, 2021

    @oresch

    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

  2. nicocins commented on Jul 21, 2021

    @nicocins
    Author

    Thanks for your help ! i ll try !

  3. tgoeminne commented on Sep 7, 2022

    @tgoeminne

    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.

  4. dereuromark commented on Jan 17, 2023

    @dereuromark
    Member

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

  5. dereuromark commented on May 11, 2026

    @dereuromark
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions