Upgrade.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace addons\RfDemo;
  3. use Yii;
  4. use common\helpers\StringHelper;
  5. use common\components\Migration;
  6. use common\interfaces\AddonWidget;
  7. use addons\RfDemo\common\models\Cate;
  8. /**
  9. * 升级数据库
  10. *
  11. * Class Upgrade
  12. * @package addons\RfDemo
  13. */
  14. class Upgrade extends Migration implements AddonWidget
  15. {
  16. /**
  17. * @var array
  18. */
  19. public $versions = [
  20. '1.0.0', // 默认版本
  21. '1.0.1',
  22. ];
  23. /**
  24. * @param $addon
  25. * @return mixed|void
  26. * @throws \yii\db\Exception
  27. */
  28. public function run($addon)
  29. {
  30. switch ($addon->version) {
  31. case '1.0.1' :
  32. $models = Cate::find()->select(['id', 'tree'])->asArray()->all();
  33. foreach ($models as $model) {
  34. $this->updateTree($model['id'], $model['tree'], Cate::class);
  35. }
  36. break;
  37. }
  38. }
  39. /**
  40. * @param int $id
  41. * @param string $tree
  42. * @param $model
  43. * @return void
  44. */
  45. protected function updateTree($id, $tree, $model)
  46. {
  47. $tree = StringHelper::replace(' ', '', $tree);
  48. $endTree = substr($tree, strlen($tree) - 1);
  49. if ($endTree != '-') {
  50. $tree = $tree.'-';
  51. $model::updateAll(['tree' => $tree], ['id' => $id]);
  52. }
  53. }
  54. }
粤ICP备19079148号