本书未发布
76. 回复模型
简介
在本章里,我们将完成回复的创建、显示和删除功能。在本节里,我们首先完成回复模型的创建和数据填充功能,为下一节开发回复列表做准备。
需求分解
在本节里,我们需要完成回复表的设计、数据模型的创建和模拟数据的填充。在填充数据时,我们打算随机给所有话题总共模拟 1000 条回复记录。
整理字段
字段名称 | 描述 | 字段类型 | 加索引缘由 | 其他 |
---|---|---|---|---|
topic_id |
话题 ID | 整数(int) | 数据关联 | signed => false, 'default => 0, null => false |
user_id |
用户 ID | 整数(int) | 数据关联 | signed => false, 'default => 0, null => false |
content |
回复内容 | 文本(text) | 不需要 | 无 |
字典讲解:
signed => false
—— 设置整数类型为非负整数 (或称之为无符号整数);default => xx
—— 为字段添加默认值;null => true/false
—— 设置字段是否允许为null。
迁移文件
使用命令工具创建数据库迁移文件:
$ php think migrate:create CreateTableReply
按上面设计的数据库表字段创建数据表,把生成的迁移文件代码改成如下所示:
database/migrations/xxxxxxxxxxxxxx_create_table_reply.php
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class CreateTableReply extends Migrator
{
public function up()
{
$table = $this->table('reply',array('engine'=>'InnoDB'));
$table->addColumn('topic_id', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '话题ID'))
->addColumn('user_id', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '用户ID'))
->addColumn('content', 'text', array('comment' => '评论内容'))
->addColumn('create_time', 'integer', array('default' => 0, 'signed' => false, 'null' => false))
->addColumn('update_time', 'integer', array('default' => 0, 'signed' => false, 'null' => false))
->addIndex(['topic_id'], ['name' => 'idx_by_topic_id'])
->addIndex(['user_id', 'topic_id'], ['name' => 'idx_by_user_id_and_topic_id'])
->save();
}
public function down()
{
$this->dropTable('reply');
}
}
代码解读
查询回复内容时我们可能会通过 topic_id
和 user_id
进行搜索,但因为MySQL数据库索引是左匹配规则,所以在这里我们给 reply
表添加了两个索引:
- 索引
idx_by_topic_id
适用用于通过topic_id
搜索回复记录; - 索引
idx_by_user_id_and_topic_id
适用于通过 1)只有user_id
;2)user_id
和topic_id
组合搜索回复记录;
数据模型
使用命令工具创建话题模型文件:
$ php think make:model common/Reply
因为在本节里,我们只需要实现回复数据的模拟,所以暂时不需要修改生成的数据模型文件代码:
application/common/model/Reply.php
<?php
namespace app\common\model;
use think\Model;
class Reply extends Model
{
//
}
填充数据
首先使用命令工具创建 seed 文件:
$ php think seed:create ReplySeed
模拟回复的seed文件完整代码如下:
database/seeds/ReplySeed.php
<?php
use Faker\Factory;
use think\migration\Seeder;
use app\common\model\User;
use app\common\model\Topic;
use app\common\model\Reply;
class ReplySeed extends Seeder
{
public function run()
{
$faker = Factory::create('zh_CN');
// 查询出所有User IDs
$user_ids = User::all()->column('id');
// 查询出所有Reply IDs
$topic_ids = Topic::all()->column('id');
$i = 0;
while($i < 1000){
// 随机取一个月以内的时间
$update_time = $faker->dateTimeThisMonth();
// 传参为生成最大时间不超过,因为创建时间需永远比更改时间要早
$create_time = $faker->dateTimeThisMonth($update_time);
$data = [
'content' => $faker->text(),
'user_id' => $faker->randomElement($user_ids),
'topic_id' => $faker->randomElement($topic_ids),
'create_time' => $create_time->getTimestamp(),
'update_time' => $update_time->getTimestamp(),
];
$reply = new Reply($data);
$reply->save();
$i++;
}
}
}
填充数据
使用以下命令填充回复数据:
$ php think seed:run -s TopicSeed
效果预览
用数据库图形工具查看 reply
表数据效果如下:
Git 版本控制
下面把代码纳入到版本管理:
$ git add -A
$ git commit -m "回复数据"