Skip to content
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
Closed

fetchAssoc() fails with freshly created Reader #213

shadowhand opened this issue Feb 21, 2017 · 7 comments

Comments

@shadowhand
Copy link
Member

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
Copy link
Member Author

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
Copy link
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.

@shadowhand
Copy link
Member Author

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

nyamsprod pushed a commit that referenced this issue Feb 22, 2017
- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
@nyamsprod
Copy link
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.

nyamsprod added a commit that referenced this issue Feb 22, 2017
- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
@nyamsprod
Copy link
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

nyamsprod added a commit that referenced this issue Feb 23, 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
- Bug fix StreamIterator::fputcsv
@nyamsprod
Copy link
Member

version 8.2.1 is out with the fix

@shadowhand
Copy link
Member Author

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
Projects
None yet
Development

No branches or pull requests

2 participants