我正在使用 CakePHP v3.x,我试图弄清楚如何通过迁移工具插入一些记录。文档仅列出修改架构的方法。我需要使用原始 SQL 手动插入记录吗?
CakePHP 3 的迁移插件是一个 Phinx 包装插件,因此可以使用以下命令添加记录up()
方法:-
public function up() {
// Save records to the newly created schema
}
public function down() {
// Remove records
}
例如,您可以使用添加新用户TableRegistry
on up
:-
public function up() {
// Save records to the newly created schema
$UsersTable = TableRegistry::get('Users');
$user = $UsersTable->newEntity();
$user->name = 'Joe Bloggs';
$user->email = '[email protected]';
$UsersTable->save($user);
}
如果使用TableRegistry
不要忘记包括use Cake\ORM\TableRegistry;
在迁移文件的顶部。
对于 CakeDC 的迁移插件,您可以使用以下命令插入记录回调在相关的迁移文件中:-
public function after($direction) {
if ($direction === 'up') {
// Save records to the newly created schema
} elseif ($direction === 'down') {
// Remove records
}
}
注意:如果您使用 Postgres 驱动程序,当前有一个bug这需要一个小的解决方法才能完成这项工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)