Skip to content

Latest commit

 

History

History
233 lines (168 loc) · 5.34 KB

File metadata and controls

233 lines (168 loc) · 5.34 KB

数据迁移与模型使用

Phinx 数据库迁移

本项目使用 Phinx 作为数据库迁移工具,用于管理数据库结构变更。

迁移文件位置

所有迁移文件位于 resource/database/migrations/ 目录下。

常用命令

根据 phinx.php 配置文件中的说明,项目使用 composer 命令来执行 Phinx 操作:

# 创建迁移文件
composer phinx create 迁移名称

# 执行迁移
composer phinx migrate

# 回滚迁移
composer phinx rollback

# 查看迁移状态
composer phinx status

# 创建种子数据
composer phinx seed:create 种子名称

# 执行所有种子数据
composer phinx seed:run

# 执行特定种子数据
composer phinx seed:run -s 种子名称

迁移示例

项目中实际使用的迁移示例如下(以管理员表为例):

<?php

declare(strict_types=1);

use app\components\BaseMigration;

final class CreateAdmin extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('admin', ['comment' => '管理员表'])
            ->addColumn('username', 'string', ['comment' => '用户名', 'null' => false, 'limit' => 64])
            ->addColumn('password', 'string', ['comment' => '密码', 'null' => false, 'limit' => 100])
            ->addColumn('name', 'string', ['comment' => '名称', 'null' => false, 'limit' => 64])
            ->addColumn('access_token', 'string', ['comment' => 'Access Token', 'null' => true, 'limit' => 100]);
        
        // 使用 BaseMigration 提供的通用字段方法添加常用列
        $this->addCommonColumns($table, [
            'status', 'created_at', 'updated_at', 'deleted_at',
        ]);
        
        // 添加索引
        $table->addIndex(['username'], ['unique' => true]);
        $table->addIndex(['access_token'], ['unique' => true]);
        
        $table->create();
    }
}

BaseMigration 通用字段

BaseMigration 类提供了常用的字段方法 addCommonColumns,可以方便地添加以下通用字段:

  • sort: 排序字段
  • status: 状态字段
  • created_at: 创建时间
  • updated_at: 更新时间
  • deleted_at: 删除时间(软删除)
  • created_by: 创建人
  • updated_by: 更新人

使用方式:

$this->addCommonColumns($table, ['status', 'created_at', 'updated_at']);

模型使用

项目使用 Laravel Eloquent ORM 作为数据模型层,所有模型都应继承 BaseModel

创建模型

使用内置命令快速创建模型:

php artisan make:model "app\\model\\User" users

这将创建一个 User 模型并自动关联 users 表。命令会自动生成基础模型代码,并自动调用 make:model-doc 命令为模型生成属性注释。

创建的模型代码如下:

<?php

namespace app\model;

use app\components\BaseModel;

class User extends BaseModel
{
    protected $table = 'users';
    protected $primaryKey = 'id';
}

生成模型文档注释

项目提供了 make:model-doc 命令,可以根据数据表结构自动生成模型的属性注释:

# 为单个模型生成文档注释
php artisan make:model-doc "app\\model\\User"

# 为指定目录下的所有模型生成文档注释
php artisan make:model-doc "app\\model"

该命令会自动读取数据库表结构,并为模型生成类似以下的注释:

/**
 * app\model\User
 *
 * @property int $id ID(主键)
 * @property string $username 用户名
 * @property string $email 邮箱
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 修改时间
 */
class User extends BaseModel
{
    // ...
}

同时,命令还会自动更新模型的 $primaryKey 属性为表的主键字段。

模型示例

实际项目中生成的模型示例:

<?php

namespace app\model;

use app\components\BaseModel;

/**
 * app\model\User
 *
 * @property int $id ID(主键)
 * @property string $username 用户名
 * @property string $email 邮箱
 * @property string $password 密码
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 修改时间
 * @property string|null $deleted_at 删除时间
 */
class User extends BaseModel
{
    protected $table = 'users';
    protected $primaryKey = 'id';
    
    protected $fillable = [
        'username',
        'email',
        'password',
        'status',
    ];
    
    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];
}

常用操作

use app\model\User;

// 查询所有
$users = User::query()->all();

// 条件查询
$user = User::query()->where('username', 'example')->first();

// 创建记录
$user = User::query()->create([
    'username' => 'example',
    'name' => 'Example',
]);

// 更新记录
$user->update(['name' => 'New Example']);

// 删除记录(软删除)
$user->delete();

// 恢复软删除的记录
$user->restore();

// 永久删除
$user->forceDelete();

// 查询包含软删除的记录
$users = User::query()->withTrashed()->get();

// 只查询软删除的记录
$users = User::query()->onlyTrashed()->get();

注意:为了更好的代码提示和 phpstan 代码类型检查,所有的模型查询代码都应该使用 ::query() 开始