New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchAssoc() fails with freshly created Reader #213

Closed
shadowhand opened this Issue Feb 21, 2017 · 7 comments

Comments

Projects
None yet
2 participants
@shadowhand
Member

shadowhand commented Feb 21, 2017

Issue summary

I need to pre-process a CSV file that was given to me that contains a number of columns with duplicate names. In order to process this file I am creating a reader, writing some columns with a writer, and then getting a reader:

$reader = Reader::createFromPath('path/to/file.csv');
$write = Writer::createFromStream(fopen('php://temp', 'r+'));
$columns_to_extract = [1, 2, 3, 4, 5, 43];

foreach ($reader->fetch() as $row) {
    $write->insertOne(array_intersect_key($row, $columns_to_extract));
}

$reader = $write->newReader()->setOffset(0);

And then I pass the $reader into another class that uses fetchAssoc():

function process(Reader $csv)
{
    foreach ($csv->fetchAssoc() as $row) {
        // do stuff with row
    }
}

This works fine for other CSVs, but as soon as I add this pre-processing step, I get the following error:

InvalidArgumentException: the specified row does not exist or is empty in importer/vendor/league/csv/src/Reader.php on line 338

Call Stack:
    0.0002     233496   1. {main}() importer/bin/import:0
    0.0255    1522728   2. League\Csv\Reader->fetchAssoc() importer/bin/import:17
    0.0255    1522928   3. League\Csv\Reader->getAssocKeys() importer/vendor/league/csv/src/Reader.php:247
    0.0255    1523208   4. League\Csv\Reader->getRow() importer/vendor/league/csv/src/Reader.php:281

System informations

(In case of a bug report Please complete the table below)

Information Description
League\Csv version 8.2.0
PHP/HHVM version 5.6.29
OS Platform osx

Standalone code, or other way to reproduce the problem

See description.

Expected result

No error.

Actual result

Exception thrown.

@shadowhand

This comment has been minimized.

Show comment
Hide comment
@shadowhand

shadowhand Feb 21, 2017

Member

For whatever reason, the following code works:

foreach ($reader->fetch() as $row) {
    $write->insertOne(array_intersect_key($row, $columns_to_extract));
}

$reader = Reader::createFromString((string) $write);
Member

shadowhand commented Feb 21, 2017

For whatever reason, the following code works:

foreach ($reader->fetch() as $row) {
    $write->insertOne(array_intersect_key($row, $columns_to_extract));
}

$reader = Reader::createFromString((string) $write);
@nyamsprod

This comment has been minimized.

Show comment
Hide comment
@nyamsprod

nyamsprod Feb 21, 2017

Member

@shadowhand it works because the latter uses SplTempFileObject while the first example uses a userland StreamIterator to enable Writer::createFromStream. I'll review your fix and merge it since I have to release a patch version with other fixes hopefully later this week or next week.

Member

nyamsprod commented Feb 21, 2017

@shadowhand it works because the latter uses SplTempFileObject while the first example uses a userland StreamIterator to enable Writer::createFromStream. I'll review your fix and merge it since I have to release a patch version with other fixes hopefully later this week or next week.

@shadowhand

This comment has been minimized.

Show comment
Hide comment
@shadowhand

shadowhand Feb 21, 2017

Member

My "fix" appears to break other things. :) I'll trust you can sort it out.

Member

shadowhand commented Feb 21, 2017

My "fix" appears to break other things. :) I'll trust you can sort it out.

nyamsprod added a commit that referenced this issue Feb 22, 2017

Bug fix #213 and #208
- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
@nyamsprod

This comment has been minimized.

Show comment
Hide comment
@nyamsprod

nyamsprod Feb 22, 2017

Member

@shadowhand while waiting for the patch in your code if you change

$write = Writer::createFromStream(fopen('php://temp', 'r+'));
//by
$write = Writer::createFromFileObject(new SplTempFileObject());

It will work.

Member

nyamsprod commented Feb 22, 2017

@shadowhand while waiting for the patch in your code if you change

$write = Writer::createFromStream(fopen('php://temp', 'r+'));
//by
$write = Writer::createFromFileObject(new SplTempFileObject());

It will work.

nyamsprod added a commit that referenced this issue Feb 22, 2017

Bug fix #213 and #208
- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
@nyamsprod

This comment has been minimized.

Show comment
Hide comment
@nyamsprod

nyamsprod Feb 22, 2017

Member

@shadowhand the PR #215 should fix your code could you try it please if it's ok for you I'll release the patch

Member

nyamsprod commented Feb 22, 2017

@shadowhand the PR #215 should fix your code could you try it please if it's ok for you I'll release the patch

nyamsprod added a commit that referenced this issue Feb 23, 2017

Bug fix #213 and #208 (#215)
* Bug fix #213 and #208

- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
- Bug fix StreamIterator::fputcsv
@nyamsprod

This comment has been minimized.

Show comment
Hide comment
@nyamsprod

nyamsprod Feb 23, 2017

Member

version 8.2.1 is out with the fix

Member

nyamsprod commented Feb 23, 2017

version 8.2.1 is out with the fix

@nyamsprod nyamsprod closed this Feb 23, 2017

@shadowhand

This comment has been minimized.

Show comment
Hide comment
@shadowhand

shadowhand Feb 25, 2017

Member

Just confirmed that this fix works. Thanks!

Member

shadowhand commented Feb 25, 2017

Just confirmed that this fix works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment