20221109015711_update_customer_portrait_field_and_select.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. use think\migration\Migrator;
  3. use think\migration\db\Column;
  4. use app\model\CustomerPortraitFieldSelect;
  5. use app\model\CustomerPortraitField;
  6. class UpdateCustomerPortraitFieldAndSelect extends Migrator
  7. {
  8. /**
  9. * Change Method.
  10. *
  11. * Write your reversible migrations using this method.
  12. *
  13. * More information on writing migrations is available here:
  14. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  15. *
  16. * The following commands can be used in this method and Phinx will
  17. * automatically reverse them when rolling back:
  18. *
  19. * createTable
  20. * renameTable
  21. * addColumn
  22. * renameColumn
  23. * addIndex
  24. * addForeignKey
  25. *
  26. * Remember to call "create()" or "update()" and NOT "save()" when working
  27. * with the Table class.
  28. */
  29. public function change()
  30. {
  31. $pids = CustomerPortraitField::where([['keyname','=','add_wechat_type']])->column('id');
  32. $query[] = ['pid','in',$pids];
  33. $select = CustomerPortraitFieldSelect::where($query)->select()->toArray();
  34. $arr = [];
  35. foreach ($select as $v) {
  36. $arr[$v['pid']][] = $v;
  37. }
  38. foreach ($arr as $k2 => $v2) {
  39. $row = array_column($v2,'name');
  40. if (!in_array('历史加微',$row)) {
  41. CustomerPortraitFieldSelect::create([
  42. 'name' => '历史加微',
  43. 'pid' => $k2
  44. ]);
  45. }
  46. }
  47. }
  48. }