CakePHP 3的Migration插件是phinx包装器插件,因此可以使用以下up()
方法添加记录:
public function up() {
// Save records to the newly created schema
}
public function down() {
// Remove records
}
例如,你可以使用添加新用户TableRegistry
上up
: -
public function up() {
// Save records to the newly created schema
$UsersTable = TableRegistry::get('Users');
$user = $UsersTable->newEntity();
$user->name = 'Joe Bloggs';
$user->email = 'joe@example.com';
$UsersTable->save($user);
}
如果使用,TableRegistry
请不要忘use Cake\ORM\TableRegistry;
了在迁移文件的顶部添加。
对于CakeDC的Migration插件,您可以使用回调在相关迁移文件中插入记录:-
public function after($direction) {
if ($direction === 'up') {
// Save records to the newly created schema
} elseif ($direction === 'down') {
// Remove records
}
}