Writes each received array as a line of a CSV file. As a blocking task, it waits until all inputs have been received and then outputs the file path.
- Service:
CleverAge\ProcessBundle\Task\File\Csv\CsvWriterTask - Blocking task
array: an associative array whose keys match the headers and values are convertible to string. Values that are
arrays are imploded using the split_character option. Underlying method is
fputcsv.
string: path of the written file (with placeholders replaced), once all inputs have been processed.
| Code | Type | Required | Default | Description |
|---|---|---|---|---|
file_path |
string |
X | Path of the file to write to (absolute or relative to the current working directory). Placeholders {date}, {date_time}, {timestamp} and {unique_token} are replaced in the path |
|
delimiter |
string |
; |
CSV delimiter | |
enclosure |
string |
" |
CSV enclosure character | |
escape |
string |
\ |
CSV escape character | |
headers |
array|null |
null |
Static list of CSV headers. If null, the keys of the first input are used |
|
mode |
string |
wb |
File open mode (see fopen mode parameter) | |
split_character |
string |
| |
Used to implode array values | |
write_headers |
bool |
true |
Write the headers as first line, only if the file is empty (useful with an append mode) |
- Write a CSV file with a date in its name
# Task configuration level
entry:
service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask'
options:
output:
- column1: value1-1
column2: value2-1
- column1: value1-2
column2: value2-2
outputs: [write]
write:
service: '@CleverAge\ProcessBundle\Task\File\Csv\CsvWriterTask'
options:
file_path: '%kernel.project_dir%/var/data/csv_writer_{date_time}.csv'- Append lines to an existing file, with fixed headers
# Task configuration level
write:
service: '@CleverAge\ProcessBundle\Task\File\Csv\CsvWriterTask'
options:
file_path: '%kernel.project_dir%/var/data/export.csv'
mode: 'ab'
headers: [sku, name, price]{date}is replaced byYmd,{date_time}byYmd_His,{timestamp}by the Unix timestamp and{unique_token}by auniqid()value.- The parent directory of the file is created if needed.
- Each input must contain every header key (extra keys are not allowed: the number of columns must match the number of
headers), otherwise an
\UnexpectedValueExceptionis thrown. Columns are written in the headers order.