【问题标题】:How to seed database in zend framework 3 (doctrine/migrations)?如何在 zend 框架 3(教义/迁移)中播种数据库?
【发布时间】:2017-11-07 11:51:59
【问题描述】:

我正在创建如下迁移。我找不到播种数据库的方法。 在 laravel 中寻找类似 db:seed 的东西?

public function up(Schema $schema)
{
    $table = $schema->createTable('user_map');
    $table->addColumn('id', 'integer', ['autoincrement'=>true]); 
    $table->addColumn('user_id', 'integer', ['notnull'=>true]);
    $table->addColumn('relation_id', 'integer', ['notnull'=>true]);
    $table->setPrimaryKey(['id']);
    $table->addOption('engine' , 'InnoDB');       
}

【问题讨论】:

  • 您是在使用config/cli-config.php 文件以独立运行您的迁移,还是使用其他东西来集成您的学说迁移?你也看过github.com/doctrine/data-fixtures 吗?

标签: php doctrine zend-framework3


【解决方案1】:

在迁移时创建的表中插入数据的一种方法是在 postUp() 方法中进行。只需添加您的迁移类:

public function postUp(Schema $schema)
{
    $this->connection->insert('user_map', array(
        'user_id' => 1,
        'relation_id' => 2
    ));
    parent::postUp($schema);
}

Zend 3 migrations documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 2023-04-09
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2013-09-16
    • 2014-07-08
    • 1970-01-01
    相关资源
    最近更新 更多