Skip to content

Commit 619af34

Browse files
committedMar 16, 2018
fix
1 parent 442be53 commit 619af34

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed
 

‎README.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ supports array parameter with following attributes
1818

1919
##### fields[]
2020
- `attribute` - attribute name from Your ActieRecord class
21-
- `value` - if callable passed it will receive current row, and return value will be save to AR, otherwise it will find element which key is passed value in current row
21+
- `value` - if callable passed it will receive current row, and return value will be saved to AR, otherwise it will find element which key is passed value in current row
2222

2323
#### validate
24-
validates each populated AR model, and returns false if there's any error, otherwise it will return tru
24+
validates each populated AR model, and returns false if there's any error, otherwise it will return true
2525

2626
#### save
2727
Saves populated AR models, and returns an array of each saved AR model's primary key
@@ -38,8 +38,10 @@ Will return array of populated AR models
3838
- Define Fields
3939

4040
```php
41+
uploadedFile = \yii\web\UploadedFile::getInstanceByName('file');
42+
4143
$importer = new \Gevman\Yii2Excel\Importer([
42-
'filePath' => '@webroot/1521226822.xlsx',
44+
'filePath' => $uploadedFile->tempName,
4345
'activeRecord' => Product::class,
4446
'scenario' => Product::SCENARIO_IMPORT,
4547
'skipFirstRow' => true,
@@ -86,7 +88,23 @@ $importer = new \Gevman\Yii2Excel\Importer([
8688
],
8789
[
8890
'attribute' => 'photos',
89-
'value' => 11,
91+
'value' => function ($row) {
92+
$photos = [];
93+
foreach (StringHelper::explode(strval($row[11]), ',', true, true) as $photo) {
94+
if (filter_var($photo, FILTER_VALIDATE_URL)) {
95+
$file = @file_get_contents($photo);
96+
if ($file) {
97+
$filename = md5($file) . '.jpg';
98+
file_put_contents(Yii::getAlias("@webroot/gallery/$filename"), $file);
99+
$photos[] = $filename;
100+
}
101+
} else {
102+
$photos[] = $photo;
103+
}
104+
}
105+
106+
return implode(',', $photos);
107+
}
90108
],
91109
[
92110
'attribute' => 'currency',

0 commit comments

Comments
 (0)