在 Doctrine 迁移中运行自定义 SQL 时阻止 "Migration executed but did not result in any SQL statements" 消息

Prevent "Migration executed but did not result in any SQL statements" message when rnning custom SQL in Doctrine migration

我正在我的 Doctrine 迁移中执行手动准备好的语句。我这样做是因为我需要最后插入的 ID 来构建进一步的相关查询。使用标准 addSql 方法不允许这样做,因为它 "lining up" 查询的性质,然后在最后执行它们。

有什么方法可以防止显示以下错误消息?

Migration 20151102112832 was executed but did not result in any SQL statements.

执行了很多SQL条语句,但是在调用我的这个方法时执行的是:

/**
 * @param string $sql
 *
 * @throws DBALException
 */
private function runSql($sql)
{
    $this->write($sql);
    $stmt = $this->connection->prepare($sql);
    $stmt->execute();
}

您可以创建 "do nothing" sql:

public function up(Schema $schema)
{
    $this->addSql('SELECT 1');
}

我认为这里最好的选择是展示您的迁移正在做什么。 您可以通过发送带有注释字符串的 sql 请求来执行此操作并抑制警告消息。例如:

public function up(Schema $schema)
{
    // Do not show warning on migrations.
    $this->addSql('# Updating entities.');
}

doctrine 迁移版本 3 在 MySQL5.7 中抛出错误,当我使用时:

public function up(Schema $schema)
{
    $this->addSql('SELECT 1');
}

错误信息:

[error] Migration DoctrineMigrations\Version20190823142208 failed during Post-Checks. Error: "An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute."

使用注释时可以修复此错误:

public function up(Schema $schema)
{
    $this->addSql('#prevent warning' ); //Prevent “Migration executed but did not result in any SQL statements”
}