20221101090632_add_community_per_to_permission_table.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. use think\migration\Migrator;
  3. use think\migration\db\Column;
  4. use app\model\Permission;
  5. use app\model\Grant;
  6. class AddCommunityPerToPermissionTable 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. $pid = (new Permission())->where('auth_name', '=', '素材中心')->value('id');
  32. $data['pid'] = $pid;
  33. $data['auth_name'] = '小区管理';
  34. $data['uri'] = 'material/community';
  35. $data['is_menu'] = 1;
  36. $data['sort'] = 50;
  37. $data['relation'] = 'material/community';
  38. $result = Permission::create($data);
  39. $id = $result->id;
  40. $grant = Grant::find(1);
  41. $permission = array_merge(json_decode(json_encode($grant->permission), true), [intval($id)]);
  42. asort($permission);
  43. $grant->permission = array_values(array_filter(array_unique($permission)));
  44. $grant->save();
  45. }
  46. }