M240627052138QimallRbacAuthItemUpdate.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\migrations;
  3. use yii\db\Migration;
  4. use common\models\rbac\AuthItem;
  5. /**
  6. * Class M240627052138QimallRbacAuthItemUpdate
  7. */
  8. class M240627052138QimallRbacAuthItemUpdate extends Migration
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function safeUp()
  14. {
  15. try {
  16. // 将id=2823的记录中的name更新为mall/setting
  17. $record = AuthItem::findOne(2823);
  18. if ($record !== null) {
  19. $record->name = 'mall/setting';
  20. $record->save();
  21. }
  22. // 更新name=mall/setting/memory的记录的pid为2823
  23. AuthItem::updateAll(['pid' => 2823], ['name' => 'mall/setting/memory']);
  24. // 查找所有name=mall/setting的记录
  25. $settingRecords = AuthItem::find()
  26. ->where(['name' => 'mall/setting'])
  27. ->all();
  28. // 删除第二个name=mall/setting的记录
  29. if (count($settingRecords) > 1) {
  30. $settingRecords[1]->delete();
  31. }
  32. } catch (\Exception $e) {
  33. \Yii::error('更新权限菜单错误:' . $e->getMessage());
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function safeDown()
  40. {
  41. echo "M240627052138QimallRbacAuthItemUpdate cannot be reverted.\n";
  42. return false;
  43. }
  44. /*
  45. // Use up()/down() to run migration code without a transaction.
  46. public function up()
  47. {
  48. }
  49. public function down()
  50. {
  51. echo "M240627052138QimallRbacAuthItemUpdate cannot be reverted.\n";
  52. return false;
  53. }
  54. */
  55. }