# CSV Export foreig table

**URL:** <https://discourse.cakephp.org/t/csv-export-foreig-table/8792>\
**Category:** Uncategorized\
**Created:** [December 28, 2020, 10:58pm UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792 "2020-12-28T22:58:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [December 28, 2020, 10:58pm UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792/1 "2020-12-28T22:58:32Z")

</div>

Hi.

I am exporting to CVS like this, but I do get an error accesing the foreign table ‘sexo’, please some help.

Thanks.

public function export()

```
{

    $this->layout = 'ajax';

    $filename = "Exportacion.csv";

    $csv_file = fopen('php://output', 'w');

    header('Content-type: application/csv');

    header('Content-Disposition: attachment; filename="'.$filename.'"');

    $results = $this->Empleados->Find('all');

    $header_row = array("identificacion", 

                        "tipo_identificacion",

                        "sexo",

                        "nombres",

                        "apellidos",

                        "fecha_nacimiento",

                        "ciudad_domicilio",

                        "direccion_domicilio",

                        "ciudad_nacimiento",

                        "fecha_expedicion",

                        "ciudad_expedicion"

                    );

    fputcsv($csv_file,$header_row,',','"');

    

    foreach($results as $result)

    {

        $row = array(

            $result->identificacion,

            $result->tipo_identificacion_id,

            $result->sexo->nombre_sexo,

            $result->nombres,

            $result->apellidos,

            $result->fecha_nacimiento,

            $result->ciudad_domicilio_id,

            $result->direccion_domicilio,

            $result->ciudad_nacimiento_id,

            $result->fecha_expedicion,

            $result->ciudad_expedicion_id

        );

        fputcsv($csv_file,$row,',','"');

    }

    fclose($csv_file);  

}
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 28, 2020, 11:17pm UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792/2 "2020-12-28T23:17:15Z")

</div>

You haven’t `contain`ed the `sexo` association when you find your `empleados`.

---

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [December 29, 2020, 2:00pm UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792/3 "2020-12-29T14:00:40Z")

</div>

> [@Zuluru](#):
>
> `contain` ed the `sexo` association when you find your `empleado`

$results = $this-\>Empleados-\>Find(‘all’)  
-\>contain([‘Sexos’,]);

Thanks. 😃

---

<div class="post-metadata">

**Author:** ![serjio](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@serjio](https://discourse.cakephp.org/u/serjio)\
**Post date:** [March 19, 2021, 11:58pm UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792/4 "2021-03-19T23:58:47Z")

</div>

I’m building my first from-scratch database in SQL Server and wanted to generate some mock data to import into one of my tables. I can import the CSV as a new table but if I try to add it to an existing table it fails due to an issue with the foreign key. The generated data for the foreign key column was within the range of numbers used in the primary key of the referenced table.

---

<div class="post-metadata">

**Author:** ![serjio](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@serjio](https://discourse.cakephp.org/u/serjio)\
**Post date:** [March 21, 2021, 9:06am UTC](https://discourse.cakephp.org/t/csv-export-foreig-table/8792/5 "2021-03-21T09:06:48Z")

</div>

> [@serjio](#):
>
> I’m building my first from-scratch database in SQL Server and wanted to generate some mock data to import into one of my tables. I can import the CSV as a new table but if I try to add it to an existing table it fails due to an issue with the foreign key. The generated data for the foreign key column was within the range of numbers used in the primary key of the referenced table [https://snaptube.cam/](https://snaptube.cam/) [https://9apps.cam/](https://9apps.cam/).

I got this done on my own,…
