forked from ruskid/yii2-csv-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARImportStrategy.php
102 lines (90 loc) · 3.41 KB
/
ARImportStrategy.php
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
<?php
/**
* @copyright Copyright Victor Demin, 2015
* @license https://github.com/ruskid/yii2-csv-importer/LICENSE
* @link https://github.com/ruskid/yii2-csv-importer#README
*/
namespace ruskid\csvimporter;
use yii\base\Exception;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\BaseImportStrategy;
/**
* Import from CSV. This will create/validate/save an ActiveRecord object per excel line.
* This is the slowest way to insert, but most reliable. Use it with small amounts of data.
*
* @author Victor Demin <[email protected]>
*/
class ARImportStrategy extends BaseImportStrategy implements ImportInterface {
/**
* ActiveRecord class name
* @var string
*/
public $className;
/**
* @throws Exception
*/
public function __construct() {
$arguments = func_get_args();
if (!empty($arguments)) {
foreach ($arguments[0] as $key => $property) {
if (property_exists($this, $key)) {
$this->{$key} = $property;
}
}
}
if ($this->className === null) {
throw new Exception(__CLASS__ . ' className is required.');
}
if ($this->configs === null) {
throw new Exception(__CLASS__ . ' configs is required.');
}
}
/**
* Will multiple import data into table
* @param array $data CSV data passed by reference to save memory.
* @return array Primary keys of imported data
*/
public function import(&$data) {
$importedPks = [];
foreach ($data as $row) {
$skipImport = isset($this->skipImport) ? call_user_func($this->skipImport, $row) : false;
if (!$skipImport) {
/* @var $model \yii\db\ActiveRecord */
$model = new $this->className;
$uniqueAttributes = [];
foreach ($this->configs as $config) {
if (isset($config['attribute']) && $model->hasAttribute($config['attribute'])) {
$value = call_user_func($config['value'], $row);
//Create array of unique attributes
if (isset($config['unique']) && $config['unique']) {
$uniqueAttributes[$config['attribute']] = $value;
}
//Set value to the model
$model->setAttribute($config['attribute'], $value);
} else
if (isset($config['virtual']) && $config['virtual']) {
//set value to virtual attribute
$value = call_user_func($config['value'], $row);
$model->{$config['attribute']} = $value;
}
}
//Check if model is unique and saved with success
if ($this->isActiveRecordUnique($uniqueAttributes) && $model->save()) {
$importedPks[] = $model->primaryKey;
}
}
}
return $importedPks;
}
/**
* Will check if Active Record is unique by exists query.
* @param array $attributes
* @return boolean
*/
private function isActiveRecordUnique($attributes) {
/* @var $class \yii\db\ActiveRecord */
$class = $this->className;
return empty($attributes) ? true :
!$class::find()->where($attributes)->exists();
}
}