Upgrade.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace addons\TinyBlog;
  3. use Yii;
  4. use common\components\Migration;
  5. use common\interfaces\AddonWidget;
  6. use common\helpers\StringHelper;
  7. use addons\TinyBlog\common\models\Cate;
  8. /**
  9. * 升级数据库
  10. *
  11. * Class Upgrade
  12. * @package addons\TinyBlog
  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. '1.0.2',
  23. ];
  24. /**
  25. * @param $addon
  26. * @return mixed|void
  27. * @throws \yii\db\Exception
  28. */
  29. public function run($addon)
  30. {
  31. switch ($addon->version) {
  32. case '1.0.2' :
  33. $models = Cate::find()->select(['id', 'tree'])->asArray()->all();
  34. foreach ($models as $model) {
  35. $this->updateTree($model['id'], $model['tree'], Cate::class);
  36. }
  37. break;
  38. case '1.0.1' :
  39. $this->addColumn('{{%addon_tiny_blog_tag}}', 'frequency', "int(10) NULL DEFAULT '0' COMMENT '使用次数'");
  40. break;
  41. }
  42. }
  43. /**
  44. * @param int $id
  45. * @param string $tree
  46. * @param $model
  47. * @return void
  48. */
  49. protected function updateTree($id, $tree, $model)
  50. {
  51. $tree = StringHelper::replace(' ', '', $tree);
  52. $endTree = substr($tree, strlen($tree) - 1);
  53. if ($endTree != '-') {
  54. $tree = $tree.'-';
  55. $model::updateAll(['tree' => $tree], ['id' => $id]);
  56. }
  57. }
  58. }
粤ICP备19079148号