您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Cakephp:如何使用迁移插入记录

Cakephp:如何使用迁移插入记录

CakePHP 3的Migration插件phinx包装器插件,因此可以使用以下up()方法添加记录

public function up() {
    // Save records to the newly created schema
}

public function down() {
    // Remove records
}

例如,你可以使用添加新用户TableRegistryup: -

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
    }
}

注意:如果您使用的是Postgres驱动程序,则当前存在一个错误,需要一个较小的解决方法才能完成此工作。

php 2022/1/1 18:39:10 有302人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶