在 Phinx 中编写数据迁移时如何转义字符串?

How do you escape strings when writing data Migrations in Phinx?

我在迁移中使用可能包含特殊字符的字符串进行了一些简单的更新。例如:

$this->execute("UPDATE `setting` SET `classname` = 'org\foo\Bar' WHERE `id` = 1 ");

这个问题例如,org\foo\Bar 插入 MySQL 时会将 \ 视为转义字符。对于 phinx 支持的每个数据库,我确信有一些特殊字符需要在字符串中处理,当直接使用 PDO 时,您可以通过使用准备好的语句和绑定参数来绕过。

phinx 中是否有任何本机方法来转义字符串,或者我是否需要求助于 PDO::quote()

正如 Charlotte 的 OP 评论中提到的那样,这个功能看起来并不存在。解决方法如下:

  1. 获取PDO连接
  2. 使用quote()或直接使用连接手动构建查询

这是我使用 quote()

的代码示例
public function change()
{
    $conn = $this->getAdapter()->getConnection();
    $quotedString = $conn->quote('org\foo\Bar');
    $this->execute("UPDATE `setting` SET `classname` = $quotedString WHERE `id` = 1 ");
 }