Skip to content

CSV Encoder (Serializer) is ignoring the value FALSE #27642

@ThomasLandauer

Description

@ThomasLandauer

Symfony version(s) affected: 4.1.0

Description
When a data value is false, the CSV Encoder just omits it - instead of exporting it as 0.

How to reproduce

$data = array('name'=>'foo', 'active'=>false);
$serializer = new Serializer(array(), array(new CsvEncoder()));
$csv = $serializer->serialize($data, 'csv');
dump($csv); // `foo,` instead of `foo,0`

Possible Solution
Maybe add a fifth argument to the constructor of CsvEncoder, something like false-as-0

Additional context
The origin of the problem is in fact php's fputcsv() function. I already filed a bug there, but it looks like they're not going to fix it: https://bugs.php.net/bug.php?id=76500

My point is: If you have a nullable boolean field in your database, the CSV Encoder is useless, since you can't tell false from null.
And since the (probably) main use case of the Serializer is to export some Doctrine entity, I think there should be some solution for that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions