20220831095627_update_protrait_table_data.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. use think\migration\Migrator;
  3. use think\migration\db\Column;
  4. use app\model\CustomerPortrait;
  5. class UpdateProtraitTableData extends Migrator
  6. {
  7. /**
  8. * Change Method.
  9. *
  10. * Write your reversible migrations using this method.
  11. *
  12. * More information on writing migrations is available here:
  13. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  14. *
  15. * The following commands can be used in this method and Phinx will
  16. * automatically reverse them when rolling back:
  17. *
  18. * createTable
  19. * renameTable
  20. * addColumn
  21. * renameColumn
  22. * addIndex
  23. * addForeignKey
  24. *
  25. * Remember to call "create()" or "update()" and NOT "save()" when working
  26. * with the Table class.
  27. */
  28. public function change()
  29. {
  30. (new CustomerPortrait())->select()->each(function ($item){
  31. $content = json_decode($item['fields'], true);
  32. if (isset($content['behavioral_info']['free_time'])){
  33. $content['family_info']['free_time'] = $content['behavioral_info']['free_time'];
  34. }
  35. if (isset($content['other_info']['car_price'])) {
  36. $content['family_info']['car_price'] = $content['other_info']['car_price'];
  37. }
  38. if (isset($content['other_info']['hourse_price'])) {
  39. $content['family_info']['hourse_price'] = $content['other_info']['hourse_price'];
  40. }
  41. if (isset($content['other_info']['buying_community'])) {
  42. $content['family_info']['buying_community'] = $content['other_info']['buying_community'];
  43. }
  44. if (isset($content['other_info']['peripheral_supporting'])) {
  45. $content['family_info']['peripheral_supporting'] = $content['other_info']['peripheral_supporting'];
  46. }
  47. unset($content['behavioral_info']['free_time']);
  48. unset($content['other_info']['car_price']);
  49. unset($content['other_info']['hourse_price']);
  50. unset($content['other_info']['buying_community']);
  51. unset($content['other_info']['peripheral_supporting']);
  52. $item->fields = json_encode($content);
  53. $item->save();
  54. });
  55. }
  56. }