Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

phinx

I've got some simple updates inside my migration with strings that may contain special characters. For example:

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

The problem with this for example, org\foo\Bar when inserted into MySQL treats \ as escape characters. For each DB phinx supports, I'm sure there are special characters that need to be handled in strings that when using PDO directly you'd get around by using prepared statements and binding parameters.

Is there any native way in phinx to escape strings or do I need to fall back on something like PDO::quote()?

like image 772
Ray Avatar asked Apr 18 '16 15:04

Ray


1 Answers

As alluded to in Charlotte's OP comments, it doesn't look like this feature exists. The work around is the following:

  1. Grab the the PDO connection
  2. Use the quote() or manually construct a query using the connection directly

Here's my code example using quote()

public function change()
{
    $conn = $this->getAdapter()->getConnection();
    $quotedString = $conn->quote('org\foo\Bar');
    $this->execute("UPDATE `setting` SET `classname` = $quotedString WHERE `id` = 1 ");
 }
like image 160
Ray Avatar answered Sep 29 '22 07:09

Ray