createTagTable(); $this->createArticleTable(); $this->createCommentTable(); $this->createArticleTagTable(); $this->createArticleLikeTable(); $this->createArticleFavoriteTable(); $this->createCommentLikeTable(); $this->createChapterOfflineTable(); $this->removeUpdateTimeColumn(); $this->modifyChapterTable(); $this->modifyUserTable(); $this->handleArticleNav(); } public function down() { $this->table('kg_tag')->drop()->save(); $this->table('kg_article')->drop()->save(); $this->table('kg_comment')->drop()->save(); $this->table('kg_article_tag')->drop()->save(); $this->table('kg_article_like')->drop()->save(); $this->table('kg_article_favorite')->drop()->save(); $this->table('kg_comment_like')->drop()->save(); $this->table('kg_chapter_offline')->drop()->save(); } protected function createTagTable() { $this->table('kg_tag', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'DYNAMIC', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('name', 'string', [ 'null' => false, 'default' => '', 'limit' => 50, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '名称', 'after' => 'id', ]) ->addColumn('alias', 'string', [ 'null' => false, 'default' => '', 'limit' => 50, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '别名', 'after' => 'name', ]) ->addColumn('icon', 'string', [ 'null' => false, 'default' => '', 'limit' => 100, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '图标', 'after' => 'alias', ]) ->addColumn('priority', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '优先级', 'after' => 'icon', ]) ->addColumn('published', 'integer', [ 'null' => false, 'default' => '1', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '发布标识', 'after' => 'priority', ]) ->addColumn('deleted', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '删除标识', 'after' => 'published', ]) ->addColumn('follow_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '关注数', 'after' => 'deleted', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'follow_count', ]) ->addColumn('update_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '更新时间', 'after' => 'create_time', ]) ->addIndex(['name'], [ 'name' => 'name', 'unique' => false, ]) ->create(); } protected function createArticleTable() { $this->table('kg_article', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'COMPACT', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('title', 'string', [ 'null' => false, 'default' => '', 'limit' => 100, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '标题', 'after' => 'id', ]) ->addColumn('cover', 'string', [ 'null' => false, 'default' => '', 'limit' => 100, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '封面', 'after' => 'title', ]) ->addColumn('summary', 'string', [ 'null' => false, 'default' => '', 'limit' => 255, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '摘要', 'after' => 'cover', ]) ->addColumn('tags', 'string', [ 'null' => false, 'default' => '', 'limit' => 255, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '标签', 'after' => 'summary', ]) ->addColumn('content', 'text', [ 'null' => false, 'limit' => 65535, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '内容', 'after' => 'tags', ]) ->addColumn('category_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '分类编号', 'after' => 'content', ]) ->addColumn('owner_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '用户编号', 'after' => 'category_id', ]) ->addColumn('source_type', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '来源类型', 'after' => 'owner_id', ]) ->addColumn('source_url', 'string', [ 'null' => false, 'default' => '', 'limit' => 100, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '来源网址', 'after' => 'source_type', ]) ->addColumn('allow_comment', 'integer', [ 'null' => false, 'default' => '1', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '允许评论', 'after' => 'source_url', ]) ->addColumn('featured', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '推荐标识', 'after' => 'allow_comment', ]) ->addColumn('published', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '发布标识', 'after' => 'featured', ]) ->addColumn('deleted', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '删除标识', 'after' => 'published', ]) ->addColumn('word_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '文字数', 'after' => 'deleted', ]) ->addColumn('view_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '浏览数', 'after' => 'word_count', ]) ->addColumn('comment_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '评论数', 'after' => 'view_count', ]) ->addColumn('like_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '点赞数', 'after' => 'comment_count', ]) ->addColumn('favorite_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '收藏数', 'after' => 'like_count', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'favorite_count', ]) ->addColumn('update_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '更新时间', 'after' => 'create_time', ]) ->addIndex(['category_id'], [ 'name' => 'category_id', 'unique' => false, ]) ->addIndex(['owner_id'], [ 'name' => 'owner_id', 'unique' => false, ]) ->create(); } protected function createCommentTable() { $this->table('kg_comment', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'DYNAMIC', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('content', 'string', [ 'null' => false, 'default' => '', 'limit' => 1000, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '内容', 'after' => 'id', ]) ->addColumn('parent_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '父级编号', 'after' => 'content', ]) ->addColumn('owner_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '用户编号', 'after' => 'parent_id', ]) ->addColumn('to_user_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '回复用户', 'after' => 'owner_id', ]) ->addColumn('item_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '条目编号', 'after' => 'to_user_id', ]) ->addColumn('item_type', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '条目类型', 'after' => 'item_id', ]) ->addColumn('client_type', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '终端类型', 'after' => 'item_type', ]) ->addColumn('client_ip', 'string', [ 'null' => false, 'default' => '', 'limit' => 64, 'collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4', 'comment' => '终端IP', 'after' => 'client_type', ]) ->addColumn('published', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '发布标识', 'after' => 'client_ip', ]) ->addColumn('deleted', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '删除标识', 'after' => 'published', ]) ->addColumn('reply_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '回复数', 'after' => 'deleted', ]) ->addColumn('like_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '点赞数', 'after' => 'reply_count', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'like_count', ]) ->addColumn('update_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '更新时间', 'after' => 'create_time', ]) ->addIndex(['item_id', 'item_type'], [ 'name' => 'item', 'unique' => false, ]) ->addIndex(['owner_id'], [ 'name' => 'owner_id', 'unique' => false, ]) ->addIndex(['parent_id'], [ 'name' => 'parent_id', 'unique' => false, ]) ->create(); } protected function createArticleTagTable() { $this->table('kg_article_tag', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'COMPACT', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('article_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '文章编号', 'after' => 'id', ]) ->addColumn('tag_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '标签编号', 'after' => 'article_id', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'tag_id', ]) ->addIndex(['article_id'], [ 'name' => 'article_id', 'unique' => false, ]) ->addIndex(['tag_id'], [ 'name' => 'tag_id', 'unique' => false, ]) ->create(); } protected function createArticleLikeTable() { $this->table('kg_article_like', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'COMPACT', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('article_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '文章编号', 'after' => 'id', ]) ->addColumn('user_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '标签编号', 'after' => 'article_id', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'user_id', ]) ->addIndex(['article_id', 'user_id'], [ 'name' => 'article_user', 'unique' => false, ]) ->create(); } protected function createArticleFavoriteTable() { $this->table('kg_article_favorite', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'COMPACT', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('article_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '文章编号', 'after' => 'id', ]) ->addColumn('user_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '用户编号', 'after' => 'article_id', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'user_id', ]) ->addIndex(['article_id'], [ 'name' => 'article_id', 'unique' => false, ]) ->addIndex(['user_id'], [ 'name' => 'user_id', 'unique' => false, ]) ->create(); } protected function createCommentLikeTable() { $this->table('kg_comment_like', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'COMPACT', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('comment_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '评论编号', 'after' => 'id', ]) ->addColumn('user_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '用户编号', 'after' => 'comment_id', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'user_id', ]) ->addIndex(['comment_id', 'user_id'], [ 'name' => 'comment_user', 'unique' => false, ]) ->create(); } protected function createChapterOfflineTable() { $this->table('kg_chapter_offline', [ 'id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'comment' => '', 'row_format' => 'DYNAMIC', ]) ->addColumn('id', 'integer', [ 'null' => false, 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'identity' => 'enable', 'comment' => '主键编号', ]) ->addColumn('course_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '课程编号', 'after' => 'id', ]) ->addColumn('chapter_id', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '章节编号', 'after' => 'course_id', ]) ->addColumn('start_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '开始时间', 'after' => 'chapter_id', ]) ->addColumn('end_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '结束时间', 'after' => 'start_time', ]) ->addColumn('create_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '创建时间', 'after' => 'end_time', ]) ->addColumn('update_time', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '更新时间', 'after' => 'create_time', ]) ->addIndex(['chapter_id'], [ 'name' => 'chapter_id', 'unique' => false, ]) ->addIndex(['course_id'], [ 'name' => 'course_id', 'unique' => false, ]) ->create(); } protected function removeUpdateTimeColumn() { $table = $this->table('kg_chapter_like'); if ($table->hasColumn('update_time')) { $table->removeColumn('update_time')->save(); } $table = $this->table('kg_consult_like'); if ($table->hasColumn('update_time')) { $table->removeColumn('update_time')->save(); } $table = $this->table('kg_course_favorite'); if ($table->hasColumn('update_time')) { $table->removeColumn('update_time')->save(); } $table = $this->table('kg_review_like'); if ($table->hasColumn('update_time')) { $table->removeColumn('update_time')->save(); } } protected function modifyUserTable() { $table = $this->table('kg_user'); if (!$table->hasColumn('article_count')) { $table->addColumn('article_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '文章数量', 'after' => 'course_count', ])->save(); } } protected function modifyChapterTable() { $table = $this->table('kg_chapter'); if (!$table->hasColumn('comment_count')) { $table->addColumn('comment_count', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '评论数量', 'after' => 'consult_count', ])->save(); } } protected function handleArticleNav() { $data = [ 'parent_id' => 0, 'level' => 1, 'name' => '专栏', 'target' => '_self', 'url' => '/article/list', 'position' => 1, 'priority' => 100, 'published' => 1, 'create_time' => time(), ]; $this->table('kg_nav') ->insert($data) ->save(); $nav = $this->getQueryBuilder() ->select('*') ->from('kg_nav') ->orderDesc('id') ->execute()->fetch('assoc'); $this->getQueryBuilder() ->update('kg_nav') ->set('path', ",{$nav['id']},") ->where(['id' => $nav['id']]) ->execute(); } }