dap*_*ini 1 migration codeigniter database-migration codeigniter-4
当我第一次为 table 制作迁移文件时users
,public function down()
迁移文件中的 是空的。当我运行时,php spark migrate
表users
已创建。
然后我用 生成了另一个迁移文件php spark make:migration users
,根据新的表结构做了一些调整并放入$this->forge->dropTable('users');
了public function down()
. 但是当我php spark migrate
再次运行时,该users
表没有新字段..
我正在使用 codeigniter 4 和 mysql。这是我的代码
用户模型php
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'users';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $insertID = 0;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
// added created_at and updated_at
protected $allowedFields = ['username', 'password', 'foto', 'nama', 'email', 'telepon', 'created_at', 'updated_at'];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}
Run Code Online (Sandbox Code Playgroud)
第一个迁移文件
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Users extends Migration
{
public function up()
{
// tabel users
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 7,
'auto_increment' => true,
],
'username' => [
'type' => 'VARCHAR',
'constraint' => 50,
'null' => false,
],
'password' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'profile_pic' => [
'type' => 'VARCHAR',
'constraint' => 50,
],
'nama' => [
'type' => 'VARCHAR',
'constraint' => 50,
],
'email' => [
'type' => 'VARCHAR',
'constraint' => 100,
],
'telepon' => [
'type' => 'VARCHAR',
'constraint' => 10,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('users');
}
public function down()
{
// hapus tabel users
}
}
Run Code Online (Sandbox Code Playgroud)
新的迁移文件
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Users extends Migration
{
public function up()
{
// tabel users
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 7,
'auto_increment' => true,
],
'username' => [
'type' => 'VARCHAR',
'constraint' => 50,
'null' => false,
],
'password' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'foto' => [
'type' => 'VARCHAR',
'constraint' => 50,
],
'nama' => [
'type' => 'VARCHAR',
'constraint' => 50,
],
'email' => [
'type' => 'VARCHAR',
'constraint' => 100,
],
'telepon' => [
'type' => 'VARCHAR',
'constraint' => 10,
],
'created_at DATETIME DEFAULT CURRENT_TIMESTAMP',
'updated_at DATETIME DEFAULT CURRENT_TIMESTAMP',
]);
$this->forge->addKey('id', true);
$this->forge->createTable('users');
}
public function down()
{
// hapus tabel users
$this->forge->dropTable('users');
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?任何帮助表示赞赏
当您执行 时,不会调用该down()
方法。php spark migrate
down()
当您使用 执行迁移回滚过程时,将运行该方法php spark migrate:rollback
。
在“新迁移文件”的方法$this->forge->dropTable('users');
的开头添加这行代码。up()
新的迁移文件
// ...
class Users extends Migration
{
public function up()
{
$this->forge->dropTable('users');
// ...
}
// ....
}
Run Code Online (Sandbox Code Playgroud)
该方法的目的down()
是“反转”该up()
方法中执行的所有操作。
考虑到在新迁移中,您仅重命名现有表列 ( profile_pic
-> foto
) 并添加时间戳列,因此如果指定更有意义的“迁移名称”会更有意义。
此外,不要删除并重新创建现有表,而是修改表。IE:
新的迁移文件
A.命令(创建新的迁移):
php spark make:migration alter_users_rename_profile_pic_add_timestamps
B. 生成的迁移。
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AlterUsersRenameProfilePicAddTimestamps extends Migration
{
private $tableName = "users";
public function up()
{
$this->forge->modifyColumn($this->tableName, [
"profile_pic" => [
'name' => 'foto',
'type' => 'VARCHAR',
'constraint' => 50,
]
]);
$this->forge->addColumn($this->tableName, [
'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
'updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
]);
}
public function down()
{
$this->forge->modifyColumn($this->tableName, [
"foto" => [
'name' => 'profile_pic',
'type' => 'VARCHAR',
'constraint' => 50,
]
]);
$this->forge->dropColumn($this->tableName, ["created_at", "updated_at"]);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3352 次 |
最近记录: |