59. 话题分类
简介
在本章节里,我们主要完成话题列表相关页面的开发,其中包括所有话题列表、分类话题列表和个人中心的话题列表。因为每一篇话题都必须发布在某一个分类下,即:话题依赖于分类,所以在开发话题列表之前我们需要准备好话题分类。
需求分解
在 ThinkBBS 项目里,我们准备创建以下四个分类:
- 分享 —— 分享创造,分享发现;
- 教程 —— 教程相关文章;
- 问答 —— 用户问答相关的话题;
- 公告 —— 站点公告类型的话题。
数据字典
以下是分类表数据结构:
字段名称 | 描述 | 字段类型 | 加索引缘由 | 其他 |
---|---|---|---|---|
name | 分类名 | 字符串(Strng) | 唯一索引 | limit => 50, default = '', null => false |
description | 分类描述 | 字符串(Strng) | 不需要 | default = '', null => false |
字典讲解:
name
—— 分类的名称,为字符串类型,是本表的一个索引字段;description
—— 分类描述,为字符串类型,使用 MySQL 字符串默认长度(255)个字符;default => xx
—— 为字段添加默认值;null => true/false
—— 设置字段是否允许为null。
迁移文件
使用命令工具创建数据库迁移文件:
$ php think migrate:create CreateTableCategory
按上面设计的数据库表字段创建数据表,把生成的迁移文件代码改成如下所示:
database/migrations/xxxxxxxxxxxxxx_create_table_category.php
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use app\common\model\Category;
class CreateTableCategory extends Migrator
{
public function up()
{
$table = $this->table('category',array('engine'=>'InnoDB'));
$table->addColumn('name', 'string', array('limit' => 50, 'default' => '', 'null' => false, 'comment' => '名称'))
->addColumn('description', 'string', array('default' => '', 'null' => false, 'comment' => '描述'))
->addColumn('create_time', 'integer', array('default' => 0, 'signed' => false, 'null' => true))
->addColumn('update_time', 'integer', array('default' => 0, 'signed' => false, 'null' => true))
->save();
$current_time = time();
$categories = [
[
'name' => '分享',
'description' => '分享创造,分享发现',
'create_time' => $current_time,
'update_time' => $current_time,
],
[
'name' => '教程',
'description' => '开发技巧、推荐扩展包等',
'create_time' => $current_time,
'update_time' => $current_time,
],
[
'name' => '问答',
'description' => '请保持友善,互帮互助',
'create_time' => $current_time,
'update_time' => $current_time,
],
[
'name' => '公告',
'description' => '站点公告',
'create_time' => $current_time,
'update_time' => $current_time,
],
];
Category::insertAll($categories);
}
public function down()
{
$this->dropTable('category');
}
}
代码讲解:
在创建分类表时我们使用 Category::insertAll 方法给分类表里添加了我们设计的四个分类数据。我们之所以在迁移文件里添加分类数据是因为分类数据通常在项目里不需要修改,而且在本教程里我们的介绍是话题管理。此外,大家需要注意的是在这里我们使用的是 insertAll 方法来给分类表添加数据,因为使用该方法写入数据时 不会 给实例记录的时间戳字段赋值,所以需要我们手动来赋值。
验证器
虽然我们现在在迁移文件里调用模型的insertAll方法给数据库表添加初始数据,但为了保持与其它模型统一,我们还是给分类也创建验证器类。
$ php think make:validate common/Category
新建验证器代码如下:
application/common/validate/Category.php
<?php
namespace app\common\validate;
use think\Validate;
class Category extends Validate
{
protected $rule = [
'name' => 'require|length:2,4|unique:category',
'description' => 'require|max:100',
];
protected $message = [
'name.require' => '分类名不能为空',
'name.length' => '分类名必须在2-4个字符之间',
'name.unique' => '分类名已存在',
'description.require' => '分类说明不能为空',
'description.max' => '分类说明必须在100字符以内',
];
}
数据模型
使用命令行创建分类的数据模型:
$ php think make:model common/Category
我们暂时不需要修改生成的数据模型代码,生成代码如下:
application/common/model/Category.php
<?php
namespace app\common\model;
use think\Model;
class Category extends Model
{
//
}
执行迁移命令
现在我们已经创建好话题分类的模型文件了,所以可以执行数据库迁移命令生成对应的数据库表。
$ php think migrae:run
效果展示
用数据库视图管理工具打开 category
表查看一下数据是否添加成功。
Git 版本控制
下面把代码纳入到版本管理:
$ git add -A
$ git commit -m "添加分类模型和数据"
推荐文章: