1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
---
layout: default
title: Upgrading from 7.x to 8.x
redirect_from: /upgrading/8.0/
---
# Upgrading from 7.x to 8.x
## Installation
If you are using composer then you should update the require section of your `composer.json` file.
```bash
composer require league/csv:^8.0
```
This will edit (or create) your `composer.json` file.
## Added features
### Reader::fetchPairs and Reader::fetchPairsWithoutDuplicates
To complements the Reader extract methods the following methods are added:
- `Reader:fetchPairs`
- `Reader:fetchPairsWithoutDuplicates`
Please [refer to the documentation](/reading/) for more information.
## Backward Incompatible Changes
### PHP required version
`Csv` 8.0.0 is the first major version to remove support for `PHP 5.4`.
### Remove optional argument to createFromString
In version 8.0 the optional second argument from `createFromString` is removed. If your code relied on it you can use the following snippet:
**Old code:**
```php
use League\Csv\Writer;
$writer = Writer::createFromString($str, "\r\n");
$writer->insertOne(["foo", null, "bar"]);
```
**New code:**
```php
use League\Csv\Writer;
$writer = Writer::createFromString($str);
$writer->setNewline("\r\n")
$writer->insertOne(["foo", null, "bar"]);
```
### Remove SplFileObject flags usage
In version 8.0 you can no longer set `SplFileObject` flags the following methods are remove:
- `setFlags`
- `getFlags`
The `SplFileObject` flags are normalized to have a normalized CSV filtering independent of the underlying PHP engine use (`HHVM` or `Zend`).
**Old code:**
```php
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
$csv->fetchAssoc(); //empty lines where removed
```
**New code:**
```php
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->fetchAssoc(); //empty lines are automatically removed
```
### fetchAssoc and fetchColumn return Iterator
`Reader::fetchAssoc` and `Reader::fetchColumn` no longer return an array but instead an `Iterator`.
**Old code:**
```php
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchAssoc(['lastname', 'firstname']);
echo $res[0]['lastname']; //would return the first row 'lastname' index
```
**New code:**
```php
use League\Csv\Reader;
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchAssoc(['lastname', 'firstname']);
echo iterator_to_array($res, false)[0]['lastname'];
```
### fetchAssoc callable argument
The optional callable argument from `Reader::fetchAssoc` now expects its first argument to be an `array` indexed by the submitted array keys. In all previous versions, the indexation was made after the callable had manipulated the CSV row.
**Old code:**
```php
use League\Csv\Reader;
$func = function (array $row) {
$row[1] = strtoupper($row[1]);
$row[2] = strtolower($row[2]);
return $row;
};
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchAssoc(['lastname', 'firstname'], $func);
```
**New code:**
```php
use League\Csv\Reader;
$func = function (array $row) {
$row['lastname'] = strtoupper($row['lastname']);
$row['firstname'] = strtolower($row['firstname']);
return $row;
};
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchAssoc(['lastname', 'firstname'], $func);
```
### fetchColumn callable argument
The optional callable argument from `Reader::fetchColumn` now expects its first argument to be the selected column value. In all previous versions, the callable first argument was an array.
**Old code:**
```php
use League\Csv\Reader;
$func = function (array $row) {
$row[2] = strtoupper($row[2]);
return $row;
};
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchColumn(2, $func);
```
**New code:**
```php
use League\Csv\Reader;
$func = function ($value) {
return strtoupper($value);
};
$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$res = $csv->fetchColum(2, $func);
```
## Deprecated methods in 7.0 series, removed in 8.0
- `Controls::detectDelimiterList` replaced by `Controls::fetchDelimitersOccurence`
- `Reader::query` replaced by `Reader::fetch`
## Removed methods in 8.0.0
- `Controls::setFlags`
- `Controls::getFlags`
- `QueryFilter::hasFilter`
- `QueryFilter::removeFilter`
- `QueryFilter::clearFilter`
- `QueryFilter::hasSortBy`
- `QueryFilter::removeSortBy`
- `QueryFilter::clearSortBy`
|