Last active
May 22, 2020 07:02
-
-
Save softsolution/6ed43efa843ce6d31f92d1eb90f4e9d3 to your computer and use it in GitHub Desktop.
Миграция данных c поля Listbitmask на поле Explist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
private function getListBitmaskItems($value, $items){ | |
$pos = 0; | |
$_items = []; | |
foreach ($items as $key => $item) { | |
if (substr($value, $pos, 1) == 1) { | |
$_items[] = $item; | |
} | |
$pos++; | |
if ($pos + 1 > strlen($value)) { | |
break; | |
} | |
} | |
return $_items; | |
} | |
public function run(){ | |
/* MIGRATE SCRIPT */ | |
if($this->cms_user->is_admin){ | |
//config | |
$table_update = 'con_proxy'; | |
$old_field_name = 'geo_proxy'; | |
$target_field_name = 'country';//поле explist | |
//сопоставляем значения полей geo_proxy -> country | |
$items = $this->model->selectOnly('i.id, i.'.$old_field_name.', i.'.$target_field_name)->get($table_update); | |
$listbit_values = $this->model->selectOnly('i.values')->filterEqual('i.name', $old_field_name)->getItem($table_update.'_fields', function($item, $model){ | |
$item['values'] = string_explode_list($item['values']); | |
return $item['values']; | |
}); | |
$explist_cat_id = $this->model->selectOnly('i.options')->filterEqual('i.name', $target_field_name)->getItem($table_update.'_fields', function($item, $model){ | |
$options = cmsModel::yamlToArray($item['options']); | |
return $options['cat_id']; | |
}); | |
//получаем значения расширеного списка | |
$explist_items = $this->model->selectOnly('i.id, i.title')->filterEqual('i.cat_id', $explist_cat_id)->get('explist', function($item, $model){ | |
return $item; | |
}); | |
$_explist_items = []; | |
foreach ($explist_items as $ei){ | |
$_explist_items[$ei['id']] = $ei['title']; | |
} | |
//переворачиваем массив | |
$exp_list_flipped = array_flip($_explist_items); | |
//сопоставляем и обновляем данные | |
foreach($items as $item_id => $item){ | |
$target_vals = $this->getListBitmaskItems($item[$old_field_name], $listbit_values); | |
$ids = []; | |
foreach($target_vals as $tval){ | |
//находим существующее значение в Explist | |
if(isset($exp_list_flipped[$tval])){ | |
$ids[] = $exp_list_flipped[$tval]; | |
//или добавляем новое | |
} else { | |
//добавляем новое поле в explist | |
$new_option_id = $this->model->insert('explist', array('cat_id' => $explist_cat_id, 'title' => $tval, 'is_pub' => 1)); | |
$ids[] = $new_option_id; | |
//добавляем значение в $exp_list_flipped | |
$exp_list_flipped[$tval] = $new_option_id; | |
} | |
} | |
//обновляю запись | |
$this->model->update($table_update, $item_id, array($target_field_name => $ids)); | |
} | |
//print_r('Итоговый $exp_list_flipped'); | |
//print_r($exp_list_flipped); | |
} | |
/* </MIGRATE SCRIPT */ | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment