CSV Export foreig table

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);  

}

You haven’t contained the sexo association when you find your empleados.

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

Thanks. :smiley:

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.

I got this done on my own,…