【问题标题】:cakephp 3.4 friendsofcake/csvview associated tables issuecakephp 3.4 friendsofcake/csvview 关联表问题
【发布时间】:2017-07-11 15:37:52
【问题描述】:

我目前正在尝试使用 FriendsOfCake/csvView 生成一个 csv 文件。问题是,我试图从中获取文件的表有许多关联的表(大约 11 个)。所以文件被导出看起来像这样:

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

我试图操纵数组,以便只显示前导表的名称而不是 ID,但我只是不断收到致命错误。

插件文档没有说明如何使用关联表,所以我不知道是否可能无法完成。

这是我的控制器导出功能,没什么花哨的:

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

非常感谢有关如何解决此问题的任何指导。

【问题讨论】:

    标签: csv cakephp export associations cakephp-3.4


    【解决方案1】:

    该插件使用平面数组结构。要显示关联数据,您可以决定将nest 关联数据放入单个字段,例如用户有很多帖子

    $_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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多