Implements persistence driver that can save data and load from CSV file.
This basic driver only offers the load/save. It does not offer conditions or
id-specific operations. You can only use a single persistence object with
a single file.
$p = new Persistence\Csv('file.csv');
$m = new MyModel($p);
$data = $m->export();
Alternatively you can write into a file. First operation you perform on
the persistence will determine the mode.
$p = new Persistence\Csv('file.csv');
$m = new MyModel($p);
$m->import($data);
| Methods |
public
|
__construct(string $file, array<string, mixed> $defaults = [])
|
#
|
public
|
__destruct()
|
#
|
public
|
openFile(string $mode = 'r'): void
Override this method and open handle yourself if you want to
reposition or load some extra columns on the top.
Override this method and open handle yourself if you want to
reposition or load some extra columns on the top.
Parameters
|
#
|
public
|
closeFile(): void
|
#
|
public
|
getLine(bool $reindexWithHeader): ($reindexWithHeader is true ? array<string, string> : array<int, string>)|null
Returns one line of CSV file as array.
Returns one line of CSV file as array.
|
#
|
public
|
putLine(array<int, string> $data): void
Writes array as one record to CSV file.
Writes array as one record to CSV file.
|
#
|
public
|
loadHeader(): void
When load operation starts, this will open file and read
the first line. This line is then used to identify columns.
When load operation starts, this will open file and read
the first line. This line is then used to identify columns.
|
#
|
public
|
saveHeader(Model $model): void
When load operation starts, this will open file and read
the first line. This line is then used to identify columns.
When load operation starts, this will open file and read
the first line. This line is then used to identify columns.
|
#
|
public
|
initializeHeader(array<int, string> $header): void
Remembers $this->header so that the data can be easier mapped.
Remembers $this->header so that the data can be easier mapped.
|
#
|
public
|
tryLoad(Model $model, $id): ?array
Tries to load data record, but will not fail if record can't be loaded.
Tries to load data record, but will not fail if record can't be loaded.
Overrides
|
#
|
public
|
prepareIterator(Model $model): Traversable<array<string, mixed>>
|
#
|
protected
|
insertRaw(Model $model, array $dataRaw)
|
#
|
public
|
export(Model $model, array<int, string>|null $fields = null): array<int, array<string, mixed>>
Export all DataSet.
|
#
|