Csvview - error while exporting to csv

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 it works, but when I add “Scans” table I got this message:

[ Notice (8)](javascript:void(0);): 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.

Do you have any ideas ?

Thanks in advance.

Calling contain will add a property called scans that is an array, thats causing the error.

Your query should use leftJoinWith or innerJoinWith

$details = $this->Details->find()
   ->leftJoinWith('Scans', function (Query $query) use ($idInventory) {
       return $query->where(['Scans.idInventory' => $idInventory]);
   });

Thanks for your answer :slight_smile: