Commit 33d902b8f1a29b760e3ba62584519766824ef68a
1 parent
86295c15
add converter as behaviour, add table - Details by migration
Showing
7 changed files
with
149 additions
and
12 deletions
Show diff stats
1 | +<?php | ||
2 | +namespace backend\components\parsers; | ||
3 | +use yii\multiparser\Converter; | ||
4 | + | ||
5 | +class CustomConverter extends Converter { | ||
6 | + | ||
7 | + /** | ||
8 | + * @param $value_arr - двумерный массив значений, которому нужно присвоить ключи | ||
9 | + * @param $key_array - ключи для вложенного массива | ||
10 | + * @return array - таблица с проименованными колонками | ||
11 | + */ | ||
12 | + public static function convertToAssocArray ($value_arr, $key_array, $key_prefix = '') | ||
13 | + { | ||
14 | + if ($key_prefix) { | ||
15 | + $parametrs_array = array_fill( 0, count($key_array), $key_prefix ); | ||
16 | + $key_array = array_map( function ($value, $key_prefix){ return str_replace( $key_prefix, '',$value ); }, $key_array, $parametrs_array ); | ||
17 | + } | ||
18 | + | ||
19 | + // преобразуем массив ключей (обернем в массив), для передачи его в качестве параметра в анонимную функцию для array_map | ||
20 | + // для этого увеличим размерность массива, что бы при каждом обходе массива $value_arr , функции был доступен исходный массив ключей | ||
21 | + $key_array = array_fill( 0, count($value_arr), $key_array ); | ||
22 | + //\common\components\CustomVarDamp::dumpAndDie($key_array); | ||
23 | + $result = array_map( | ||
24 | + function ($value, $key_array) { | ||
25 | + $res = $value; | ||
26 | + foreach ($value as $key => $sub_value) { | ||
27 | + if (isset($key_array[$key])) { | ||
28 | + // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим | ||
29 | + $new_key = $key_array[$key]; | ||
30 | + if( !array_key_exists( $new_key , $res ) ){ | ||
31 | + $res[ $new_key ] = $res[$key]; | ||
32 | + } | ||
33 | + } | ||
34 | + unset( $res[$key] ); | ||
35 | + } | ||
36 | + | ||
37 | + return $res; | ||
38 | + }, | ||
39 | + $value_arr, $key_array); | ||
40 | + return $result; | ||
41 | + } | ||
42 | + | ||
43 | +} | ||
0 | \ No newline at end of file | 44 | \ No newline at end of file |
backend/components/parsers/CustomCsvParser.php
@@ -16,13 +16,17 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { | @@ -16,13 +16,17 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { | ||
16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
17 | public function setupConverter() | 17 | public function setupConverter() |
18 | { | 18 | { |
19 | + if ( count($this->converter_conf) ) { | ||
20 | + | ||
21 | + if ($this->hasHeaderRow) { | ||
22 | + // если у файла есть заголовок, то в результате имеем ассоциативный массив | ||
23 | + $this->converter_conf['hasKey'] = 1; | ||
24 | + } | ||
25 | + | ||
26 | + $this->converter = \Yii::createObject($this->converter_conf); | ||
19 | 27 | ||
20 | - if ($this->hasHeaderRow) { | ||
21 | - // если у файла есть заголовок, то в результате имеем ассоциативный массив | ||
22 | - $this->converter_conf['hasKey'] = 1; | ||
23 | } | 28 | } |
24 | 29 | ||
25 | - $this->converter = \Yii::createObject($this->converter_conf); | ||
26 | 30 | ||
27 | } | 31 | } |
28 | 32 |
backend/components/parsers/config.php
@@ -4,11 +4,9 @@ | @@ -4,11 +4,9 @@ | ||
4 | ['web' => | 4 | ['web' => |
5 | ['class' => 'backend\components\parsers\CustomCsvParser', | 5 | ['class' => 'backend\components\parsers\CustomCsvParser', |
6 | 'auto_detect_first_line' => true, | 6 | 'auto_detect_first_line' => true, |
7 | - 'converter_conf' => ['class' => 'yii\multiparser\Converter', | ||
8 | - 'configuration' => [ | ||
9 | - "string" => 'DESCR' | ||
10 | - ] | ||
11 | - ,]], | 7 | + 'converter_conf' => ['class' => ' backend\components\parsers\CustomConverter', |
8 | + 'configuration' => ["string" => 'DESCR'],] | ||
9 | + ], | ||
12 | 10 | ||
13 | 'basic_column' => [ | 11 | 'basic_column' => [ |
14 | Null => 'Пусто', | 12 | Null => 'Пусто', |
backend/config/main.php
@@ -43,6 +43,9 @@ return [ | @@ -43,6 +43,9 @@ return [ | ||
43 | 43 | ||
44 | 'class' => 'yii\multiparser\YiiMultiparser', | 44 | 'class' => 'yii\multiparser\YiiMultiparser', |
45 | 'configuration' => $mp_configuration, | 45 | 'configuration' => $mp_configuration, |
46 | + 'as behavior' => [ | ||
47 | + 'class' => 'backend\components\parsers\CustomConverter', | ||
48 | + ], | ||
46 | 49 | ||
47 | ], | 50 | ], |
48 | ], | 51 | ], |
backend/controllers/ParserController.php
@@ -96,7 +96,7 @@ class ParserController extends BaseController | @@ -96,7 +96,7 @@ class ParserController extends BaseController | ||
96 | ], | 96 | ], |
97 | ]); | 97 | ]); |
98 | 98 | ||
99 | - // CustomVarDamp::dumpAndDie($data); | 99 | + //CustomVarDamp::dumpAndDie($data); |
100 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); | 100 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); |
101 | 101 | ||
102 | // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); | 102 | // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); |
@@ -117,13 +117,15 @@ public function actionWrite() | @@ -117,13 +117,15 @@ public function actionWrite() | ||
117 | $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); | 117 | $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); |
118 | } | 118 | } |
119 | 119 | ||
120 | + | ||
120 | //CustomVarDamp::dumpAndDie($model); | 121 | //CustomVarDamp::dumpAndDie($model); |
121 | if ($model->validate()) { | 122 | if ($model->validate()) { |
122 | $arr = $model->toArray(); | 123 | $arr = $model->toArray(); |
123 | $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); | 124 | $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); |
124 | 125 | ||
125 | - // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); | ||
126 | - CustomVarDamp::dumpAndDie($arr); | 126 | + $data = \Yii::$app->multiparser->convertToAssocArray( $data, $arr, 'attr_' ); |
127 | + $data[1]['BOX'] = \Yii::$app->multiparser->convertToFloat( $data[1]['BOX'] ); | ||
128 | + CustomVarDamp::dumpAndDie($data); | ||
127 | } | 129 | } |
128 | 130 | ||
129 | 131 |
1 | +<?php | ||
2 | +/** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: Cibermag | ||
5 | + * Date: 15.09.2015 | ||
6 | + * Time: 16:49 | ||
7 | + */ | ||
8 | + | ||
9 | +namespace backend\models; | ||
10 | + | ||
11 | +use yii\base\Model; | ||
12 | + | ||
13 | +class Details extends Model{ | ||
14 | + | ||
15 | + public function save ($arr_value) | ||
16 | + { | ||
17 | + | ||
18 | + } | ||
19 | +} | ||
0 | \ No newline at end of file | 20 | \ No newline at end of file |
1 | +<?php | ||
2 | + | ||
3 | +use yii\db\Schema; | ||
4 | +use yii\db\Migration; | ||
5 | + | ||
6 | +class m150915_125129_addDetails extends Migration | ||
7 | +{ | ||
8 | + public function up() | ||
9 | + { | ||
10 | + $tableOptions = null; | ||
11 | + if ($this->db->driverName === 'mysql') { | ||
12 | + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; | ||
13 | + } | ||
14 | + | ||
15 | + $this->createTable('{{%details}}', [ | ||
16 | + 'ID' => 'int(10) UNSIGNED NOT NULL', | ||
17 | + 'IMPORT_ID' => 'int(6) unsigned NOT NULL', | ||
18 | + 'BRAND' => 'varchar(100) NOT NULL', | ||
19 | + 'ARTICLE' => 'varchar(100) NOT NULL', | ||
20 | + 'FULL_ARTICLE' => 'varchar(150) NOT NULL', | ||
21 | + 'PRICE' => 'float(15,2) unsigned NOT NULL', | ||
22 | + 'DESCR' => 'varchar(200) NOT NULL', | ||
23 | + 'BOX' => 'int(6) unsigned NOT NULL', | ||
24 | + 'ADD_BOX' => 'int(6) unsigned NOT NULL DEFAULT 0', | ||
25 | + 'GROUP' => 'varchar(200) NOT NULL', | ||
26 | + 'timestamp' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',], $tableOptions); | ||
27 | + | ||
28 | + //$this->dropPrimaryKey('ID','{{%details}}'); | ||
29 | + $this->createIndex('ID_delete', '{{%details}}', 'ID', true); | ||
30 | + $this->execute('ALTER TABLE details | ||
31 | + CHANGE COLUMN ID ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT'); | ||
32 | + $this->addPrimaryKey('importer_id', '{{%details}}', 'import_id, brand, ARTICLE'); | ||
33 | + $this->createIndex('timestamp', '{{%details}}', 'timestamp', false); | ||
34 | + $this->createIndex('ARTICLE', '{{%details}}', 'ARTICLE', 'BRAND', 'ADD_BOX', false); | ||
35 | + $this->createIndex('IMPORT_ID', '{{%details}}', 'ARTICLE', false); | ||
36 | + $this->createIndex('IMPORT_ID_2', '{{%details}}', 'IMPORT_ID', 'timestamp', false); | ||
37 | + | ||
38 | + | ||
39 | + | ||
40 | + | ||
41 | +// PRIMARY KEY ('ARTICLE','BRAND','IMPORT_ID'), | ||
42 | +// UNIQUE KEY 'ID_delete' ('ID'), | ||
43 | +// KEY 'timestamp' ('timestamp'), | ||
44 | +// KEY 'ARTICLE' ('ARTICLE','BRAND','BOX'), | ||
45 | +// KEY 'BRAND' ('BRAND','ARTICLE'), | ||
46 | +// KEY 'ARTICLE_2' ('ARTICLE','BRAND','ADD_BOX'), | ||
47 | +// KEY 'IMPORT_ID' ('IMPORT_ID','ARTICLE'), | ||
48 | +// KEY 'IMPORT_ID_2' ('IMPORT_ID','timestamp | ||
49 | + | ||
50 | + } | ||
51 | + | ||
52 | + public function down() | ||
53 | + { | ||
54 | + $this->dropTable('{{%details}}'); | ||
55 | + | ||
56 | + } | ||
57 | + | ||
58 | + /* | ||
59 | + // Use safeUp/safeDown to run migration code within a transaction | ||
60 | + public function safeUp() | ||
61 | + { | ||
62 | + } | ||
63 | + | ||
64 | + public function safeDown() | ||
65 | + { | ||
66 | + } | ||
67 | + */ | ||
68 | +} |