在学说迁移中转义反斜杠

时间:2021-02-09 14:49:42

标签: doctrine-orm mysql-8.0 symfony5 php-7.4 doctrine-migrations

我有一个名为 Entity 的表。它有一个包含实体命名空间的字段,如下所示:

Old\App\Email 我正在尝试将其重命名为 New\App\Email

我得到了这个有效的 SQL 查询来重命名 MySQL 控制台中的所有行。它在条件中需要 4 个 \,在 \ 中需要 2 个 REPLACE

UPDATE Entity SET entity_name = REPLACE(entity_name, 'Old\\App', 'New\\App') WHERE entity_name LIKE 'Old\\\\App%';

让它正常工作已经很难逃脱,但是现在尝试将它添加到Doctrine Migration

$this->addSql('update log set entity_name = replace(entity_name, \'Old\', \'New\') where entity_name like \'Old\App%\'');

它在不做任何更改的情况下执行。我认为它没有正确逃脱。 我已经尝试了从 4 个反斜杠转义的所有组合。甚至尝试了引用转义的不同组合,希望这会产生预期的转义,但根本没有更改任何行

$this->addSql('update Entity set entity_name = replace(entity_name, \'Old\', \'New1\') where entity_name like \'Old\App%\'');
$this->addSql('update Entity set entity_name = replace(entity_name, \'Old\', \'New2\') where entity_name like \'Old\\App%\'');
$this->addSql('update Entity set entity_name = replace(entity_name, \'Old\', \'New3\') where entity_name like \'Old\\\App%\'');
$this->addSql('update Entity set entity_name = replace(entity_name, \'Old\', \'New4\') where entity_name like \'Old\\\\App%\'');

$this->addSql('update Entity set entity_name = replace(entity_name, "Old", "New5") where entity_name like "Old\App%"');
$this->addSql('update Entity set entity_name = replace(entity_name, "Old", "New6") where entity_name like "Old\\App%"');
$this->addSql('update Entity set entity_name = replace(entity_name, "Old", "New7") where entity_name like "Old\\\App%"');
$this->addSql('update Entity set entity_name = replace(entity_name, "Old", "New8") where entity_name like "Old\\\\App%"');

感谢其他程序员的帮助。

教义版本为 2.7

1 个答案:

答案 0 :(得分:0)

它并不漂亮,但它在 where 子句中需要 7 或 8 个 \,在替换函数中需要 4 个,用双引号和单引号括起来的搜索字符串:

$this->addSql("update Entity set entity_name = replace(entity_name, 'Old\\\\App', 'New\\\\App') where entity_name like 'Old\\\\\\\\App%'");