本书未发布

61. 话题模型

未匹配的标注

简介

在本节里,我们完成话题数据表和数据模型的创建。

需求分解

因为在本章里我们主要是开发列表页,所以在创建完话题数据表和数据模型后还需要给数据表填充一些测试数据。

数据字典

以下是话题表数据结构:

字段名称 描述 字段类型 加索引缘由 其他
title 话题标题 字符串(string) 文章搜索 'default => '', null => false
body 话题内容 文本(text) 不需要
user_id 用户 ID 整数(int) 数据关联 signed => false, 'default => 0, null => false
category_id 分类 ID 整数(int) 数据关联 signed => false, 'default => 0, null => false
reply_count 回复数量 整数(int) 文章回复数量排序 signed => false, 'default => 0, null => false
view_count 查看总数 整数(int) 文章查看数量排序 signed => false, 'default => 0, null => false
last_reply_user_id 最后回复的用户 ID 整数(int) 数据关联 signed => false, 'default => 0, null => false
excerpt 文章摘要,SEO 优化时使用 文本(text) 不需要 'default => '', null => false

字典讲解:

  • title—— 为了提高按标题关键词搜索效率,我们给该字段添加了索引;
  • user_id—— 为了提高关联用户表或查询指定用户的话题,我们给该字段添加了索引;
  • category_id—— 为了提高关联分类表或查询指定分类的话题,我们给该字段添加了索引;
  • signed => false—— 设置整数类型为非负整数 (或称之为无符号整数);
  • default => xx—— 为字段添加默认值;
  • null => true/false—— 设置字段是否允许为null。

迁移文件

使用命令工具创建数据库迁移文件:

$ php think migrate:create CreateTableTopic

按上面设计的数据库表字段创建数据表,把生成的迁移文件代码改成如下所示:

database/migrations/xxxxxxxxxxxxxx_create_table_topic.php

<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateTableTopic extends Migrator
{
    public function up()
    {
        $table = $this->table('topic',array('engine'=>'InnoDB'));
        $table->addColumn('title', 'string', array('default' => '', 'null' => false, 'comment' => '话题标题'))
            ->addColumn('body', 'text', array('comment' => '话题内容'))
            ->addColumn('user_id', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '创建用户ID'))
            ->addColumn('category_id', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '分类ID'))
            ->addColumn('reply_count', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '回复数量'))
            ->addColumn('view_count', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '查看总数'))
            ->addColumn('last_reply_user_id', 'integer', array('default' => 0, 'signed' => false, 'null' => false, 'comment' => '最后回复的用户ID'))
            ->addColumn('excerpt', 'text', array('default' => '', 'null' => false, 'comment' => '文章摘要,SEO优化时使用'))
            ->addColumn('create_time', 'integer', array('default' => 0, 'signed' => false, 'null' => false))
            ->addColumn('update_time', 'integer', array('default' => 0, 'signed' => false, 'null' => false))
            ->addIndex(['title'])
            ->addIndex(['user_id'])
            ->addIndex(['category_id'])
            ->save();
    }

    public function down()
    {
        $this->dropTable('topic');
    }
}

数据模型

因为我们在下一章详细介绍话题的创建和编辑,本节不给该模型创建验证器,所以使用命令工具创建话题的数据模型:

$ php think make:model common/Topic

生成的数据模型代码如下:

application/common/model/Topic.php

<?php

namespace app\common\model;

use think\Model;

class Topic extends Model
{

}

填充数据

首先,使用命令工具创建 seeder :

$ php think seed:create TopicSeed

数据填充类的代码如下:

database/seeds/TopicSeed.php

<?php

use Faker\Factory;
use think\migration\Seeder;
use app\common\model\User;
use app\common\model\Topic;
use app\common\model\Category;

class TopicSeed extends Seeder
{
    public function run()
    {
        $faker = Factory::create('zh_CN');
        // 查询出所有User IDs
        $user_ids = User::all()->column('id');
        // 查询出所有Category IDs
        $category_ids = Category::all()->column('id');

        $i = 0;
        while($i < 100){
            $sentence = $faker->sentence();
            // 随机取一个月以内的时间
            $update_time = $faker->dateTimeThisMonth();
            // 传参为生成最大时间不超过,因为创建时间需永远比更改时间要早
            $create_time = $faker->dateTimeThisMonth($update_time);
            $data = [
                'title' => $sentence,
                'excerpt' => $sentence,
                'body' => $faker->text(),
                'user_id' => $faker->randomElement($user_ids),
                'category_id' => $faker->randomElement($category_ids),
                'create_time' => $create_time->getTimestamp(),
                'update_time' => $update_time->getTimestamp(),
            ];

            $topic = new Topic($data);
            $topic->save();
            $i++;
        }

    }
}

代码解读

  • 在 seeder 里,我们在查询注册用户和话题分类时使用 column 方法让模型查询只返回这两个表的主键( ID );
  • 我们使用 faker->dateTimeThisMonth 为每个话题模拟一个合理的创建和更新时间,因为该方法返回的是一个 DataTime 实例对象,所以在存储时我们使用 getTimestamp 把变量值转化成时间戳。

接下来,执行命令完成数据填充:

$ php think seed:run -s TopicSeed

效果预览

用数据库视图管理工具打开 topic 表查看一下数据是否添加成功。

Git 版本控制

下面把代码纳入到版本管理:

$ git add -A
$ git commit -m "生成话题模型文件并填充数据"

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~