0

I'm currently trying to generate a csv file using FriendsOfCake/csvView. The thing is, the table I'm trying to get the file from, has many associated tables (about 11). so the file gets exported looking something like this:

CODE |   NAME   | MANUFACTURER_ID | BRAND_ID | OWNER_ID | ADDRESS   | ==
234A | John Doe |        4        |     1    |     3    | fake st 1 |

I tried to manipulate the array so that only the name of the foreing tables gets shown instead if the ID's but I just kept getting Fatal Errors.

The plugin documentation doesn't show anything about how to work with associated tables, so I don't know if maybe it can not be done.

this is my controllers export function, nothing fancy:

public function export() {
    $now = Time::now();

    $this->response->download('reporte_codigos_vin_' . $now . '.csv');
    $data = $this->VinCodes->find('all')->toArray();
    $_serialize = 'data';
    $_header = ['ID', 'Código VIN', 'Fabricante', 'Marca', 'Modelo', 'Tipo de Equipo', 'Año de Fabricación', 'País de Fabricación', 'Kilometraje', 'Nº Serie Motor Orignal', 'Marca Motor', 'Modelo Motor', 'Nº Serie Remotorización', 'Marca Motor R', 'Modelo Motor R', 'Cliente', 'País Cliente', 'Propietario del vehiculo', 'Placa del vehiculo', 'País de Ubicación del Propietario', 'Provincia de Ubicación del Propietario', 'Dirección de Ubicación del Propietario', 'Fecha de Creación', 'Fecha de Modificación'];
    $this->set(compact('data', '_serialize', '_header'));
    $this->viewBuilder()->className('CsvView.Csv');
    return;
}

This is the link in the view:

<?= $this->Html->link('Export', [
    'controller' => 'vinCodes', 
    'action' => 'export',
    '_ext' => 'csv'], ['style' => ['color: white;'], 'escape' => false, 'class' => 'btn btn-w-m btn-success btn-sm']) ?>

I'd really appreciate any guidance on how work around this problem.

ndm
  • 59,784
  • 9
  • 71
  • 110
DanielHolguin
  • 305
  • 4
  • 13

1 Answers1

0

The plugin works with a flat array structure. To display associated data, you may decide to nest the associated data into a single field e.g. Users hasMany posts

$_header = ['Username', 'Titles'];
$_extract = [
            'username',
            // 'posts.0.title'  //get title of first post using Hash
            function ($row) {
                $results = Hash::extract($row['posts'], '{n}.title');
                return implode('|', $results) ;
            }
        ];
    //output
    //Username, Titles
    //eddie, hadoop|hortonworks|mapr
zipate
  • 166
  • 1
  • 4