Doctrine specific adapter
Deprecation Warning
In a nutshell, the features provided by this package have been implemented in a better integrated manner directly into the main package, without the need for a third party package.
since version 9.16.0
you can write:
<?php
use League\Csv\Reader;
use League\Csv\Statement;
$csv = Reader::createFromPath('/path/to/my/file.csv');
$csv->setHeaderOffset(0);
$csv->setDelimiter(';');
$criteria = Statement::create()
->andWhere('prenom', '=', 'Adam')
->orderByAsc('annee')
->orderByDesc('foo')
->offset(3)
->limit(10);
$resultset = $criteria->process($csv);
Which covers all the features provided by this package.
Introduction
This extension package contains:
- a class to convert
League\Csv\Reader
instances into Doctrine Collections objects. - a class to enable using Doctrine Collections powerful Expression API on League Csv TabularReader objects.
<?php
use Doctrine\Common\Collections\Criteria;
use League\Csv\Doctrine as CsvDoctrine;
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/my/file.csv');
$csv->setHeaderOffset(0);
$csv->setDelimiter(';');
$criteria = Criteria::create()
->andWhere(Criteria::expr()->eq('prenom', 'Adam'))
->orderBy( [ 'annee' => 'ASC', 'foo' => 'desc', ] )
->setFirstResult(0)
->setMaxResults(10)
;
//you can do
$resultset = CsvDoctrine\CriteriaConverter::convert($criteria)->process($csv);
$result = new CsvDoctrine\RecordCollection($resultset);
//or
$collection = new CsvDoctrine\RecordCollection($csv);
$result = $collection->matching($criteria);
System Requirements
- doctrine/collection >= 2.0
But the latest stable version of each dependency is recommended.
Installation
composer require league/csv-doctrine
Usage
Converting a League\Csv\Reader
into a Doctrine Collection object
<?php
use League\Csv\Doctrine\RecordCollection;
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/my/file.csv');
$csv->setHeaderOffset(0);
$csv->setDelimiter(';');
$collection = new RecordCollection($csv);
Converting a League\Csv\ResultSet
into a Doctrine Collection object
<?php
$csv = Reader::createFromPath('/path/to/my/file.csv');
$csv->setHeaderOffset(0);
$csv->setDelimiter(';');
$stmt = Statement::create()
->where(fn (array $row): bool => isset($row['email']) && str_ends_with($row['email'], '@github.com'));
$collection = new RecordCollection($stmt->process($csv));
Using Doctrine Criteria to filter a League\Csv\Reader
object
<?php
use Doctrine\Common\Collections\Criteria;
use League\Csv\Doctrine\CriteriaConverter;
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/my/file.csv');
$csv->setHeaderOffset(0);
$csv->setDelimiter(';');
$criteria = Criteria::create()
->andWhere(Criteria::expr()->eq('name', 'Adam'))
->orderBy(['years', 'ASC'])
->setFirstResult(0)
->setMaxResults(10)
;
$stmt = CriteriaConverter::convert($criteria);
$resultset = $stmt->process($csv);
CriteriaConverter advanced usages
<?php
use Doctrine\Common\Collections\Criteria;
use League\Csv\Statement;
public static CriteriaConverter::convert(Criteria $criteria, Statement $stmt = null): Statement
public static CriteriaConverter::addWhere(Criteria $criteria, Statement $stmt = null): Statement
public static CriteriaConverter::addOrderBy(Criteria $criteria, Statement $stmt = null): Statement
public static CriteriaConverter::addInterval(Criteria $criteria, Statement $stmt = null): Statement
CriteriaConverter::convert
converts theCriteria
object into aStatement
object.CriteriaConverter::addWhere
adds theCriteria::getWhereExpression
filters to the submittedStatement
object.CriteriaConverter::addOrderBy
adds theCriteria::getOrderings
filters to the submittedStatement
object.CriteriaConverter::addInterval
adds theCriteria::getFirstResult
andCriteria::getMaxResults
filters to the submittedStatement
object.
WARNING: While the Criteria
object is mutable the Statement
object is immutable. All returned Statement
objects are new instances