diff --git a/backend/components/parsers/CustomConverter.php b/backend/components/parsers/CustomConverter.php new file mode 100644 index 0000000..4f3de1b --- /dev/null +++ b/backend/components/parsers/CustomConverter.php @@ -0,0 +1,43 @@ + $sub_value) { + if (isset($key_array[$key])) { + // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим + $new_key = $key_array[$key]; + if( !array_key_exists( $new_key , $res ) ){ + $res[ $new_key ] = $res[$key]; + } + } + unset( $res[$key] ); + } + + return $res; + }, + $value_arr, $key_array); + return $result; + } + +} \ No newline at end of file diff --git a/backend/components/parsers/CustomCsvParser.php b/backend/components/parsers/CustomCsvParser.php index 5674511..d58fe83 100644 --- a/backend/components/parsers/CustomCsvParser.php +++ b/backend/components/parsers/CustomCsvParser.php @@ -16,13 +16,17 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { // public $keys = ['first','second', 'third', 'forth', 'fifth']; public function setupConverter() { + if ( count($this->converter_conf) ) { + + if ($this->hasHeaderRow) { + // если у файла есть заголовок, то в результате имеем ассоциативный массив + $this->converter_conf['hasKey'] = 1; + } + + $this->converter = \Yii::createObject($this->converter_conf); - if ($this->hasHeaderRow) { - // если у файла есть заголовок, то в результате имеем ассоциативный массив - $this->converter_conf['hasKey'] = 1; } - $this->converter = \Yii::createObject($this->converter_conf); } diff --git a/backend/components/parsers/config.php b/backend/components/parsers/config.php index 6e14618..de0fcdf 100644 --- a/backend/components/parsers/config.php +++ b/backend/components/parsers/config.php @@ -4,11 +4,9 @@ ['web' => ['class' => 'backend\components\parsers\CustomCsvParser', 'auto_detect_first_line' => true, - 'converter_conf' => ['class' => 'yii\multiparser\Converter', - 'configuration' => [ - "string" => 'DESCR' - ] - ,]], + 'converter_conf' => ['class' => ' backend\components\parsers\CustomConverter', + 'configuration' => ["string" => 'DESCR'],] + ], 'basic_column' => [ Null => 'Пусто', diff --git a/backend/config/main.php b/backend/config/main.php index 386e300..390ddbc 100644 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -43,6 +43,9 @@ return [ 'class' => 'yii\multiparser\YiiMultiparser', 'configuration' => $mp_configuration, + 'as behavior' => [ + 'class' => 'backend\components\parsers\CustomConverter', + ], ], ], diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index eb6dab9..6563c8c 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -96,7 +96,7 @@ class ParserController extends BaseController ], ]); - // CustomVarDamp::dumpAndDie($data); + //CustomVarDamp::dumpAndDie($data); $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); @@ -117,13 +117,15 @@ public function actionWrite() $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); } + //CustomVarDamp::dumpAndDie($model); if ($model->validate()) { $arr = $model->toArray(); $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); - // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); - CustomVarDamp::dumpAndDie($arr); + $data = \Yii::$app->multiparser->convertToAssocArray( $data, $arr, 'attr_' ); + $data[1]['BOX'] = \Yii::$app->multiparser->convertToFloat( $data[1]['BOX'] ); + CustomVarDamp::dumpAndDie($data); } diff --git a/backend/models/Details.php b/backend/models/Details.php new file mode 100644 index 0000000..f840698 --- /dev/null +++ b/backend/models/Details.php @@ -0,0 +1,19 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%details}}', [ + 'ID' => 'int(10) UNSIGNED NOT NULL', + 'IMPORT_ID' => 'int(6) unsigned NOT NULL', + 'BRAND' => 'varchar(100) NOT NULL', + 'ARTICLE' => 'varchar(100) NOT NULL', + 'FULL_ARTICLE' => 'varchar(150) NOT NULL', + 'PRICE' => 'float(15,2) unsigned NOT NULL', + 'DESCR' => 'varchar(200) NOT NULL', + 'BOX' => 'int(6) unsigned NOT NULL', + 'ADD_BOX' => 'int(6) unsigned NOT NULL DEFAULT 0', + 'GROUP' => 'varchar(200) NOT NULL', + 'timestamp' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',], $tableOptions); + + //$this->dropPrimaryKey('ID','{{%details}}'); + $this->createIndex('ID_delete', '{{%details}}', 'ID', true); + $this->execute('ALTER TABLE details + CHANGE COLUMN ID ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT'); + $this->addPrimaryKey('importer_id', '{{%details}}', 'import_id, brand, ARTICLE'); + $this->createIndex('timestamp', '{{%details}}', 'timestamp', false); + $this->createIndex('ARTICLE', '{{%details}}', 'ARTICLE', 'BRAND', 'ADD_BOX', false); + $this->createIndex('IMPORT_ID', '{{%details}}', 'ARTICLE', false); + $this->createIndex('IMPORT_ID_2', '{{%details}}', 'IMPORT_ID', 'timestamp', false); + + + + +// PRIMARY KEY ('ARTICLE','BRAND','IMPORT_ID'), +// UNIQUE KEY 'ID_delete' ('ID'), +// KEY 'timestamp' ('timestamp'), +// KEY 'ARTICLE' ('ARTICLE','BRAND','BOX'), +// KEY 'BRAND' ('BRAND','ARTICLE'), +// KEY 'ARTICLE_2' ('ARTICLE','BRAND','ADD_BOX'), +// KEY 'IMPORT_ID' ('IMPORT_ID','ARTICLE'), +// KEY 'IMPORT_ID_2' ('IMPORT_ID','timestamp + + } + + public function down() + { + $this->dropTable('{{%details}}'); + + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} -- libgit2 0.21.4