What is the ZF2 equivalent of quoteInto () method for ZF1?

In Zend Framework 1, there is a quotinto method for a database adapter that can be used to quote sql statements.

I would like to know its equivalent in Zend Framework 2?

+7
source share
3 answers

Unfortunately, the quoteInto() method was removed with the introduction of the new Zend\Db in ZF 2.0. And there is no equivalent that has exactly the same behavior.

In ZF2, there is a quoteValue() method. This method takes a single value as a parameter and then quotes the value, so you can safely put it in an SQL query as a value.

However, you can use quoteValue() to replicate the behavior of the ZF1 quoteInto() method. You can simply take the code of the quoteInto() method from ZF1 and apply the quoteValue() method from the platform object in ZF2 to it:

 // modified quoteInto() function for ZF2 function quoteInto($text, $value, $platform, $count = null) { if ($count === null) { return str_replace('?', $platform->quoteValue($value), $text); } else { while ($count > 0) { if (strpos($text, '?') !== false) { $text = substr_replace($text, $platform->quoteValue($value), strpos($text, '?'), 1); } --$count; } return $text; } } 

There are some differences. ZF1 has the $type parameter, but because of the way ZF2 works with these things, the type parameter doesn't make much sense. And there is a $platform parameter, because this method has a platform dependency for the quoteValue() method.

+6
source

Quoting an SQL query is an old and potentially unsafe way to do this. You should use prepared statements that have much better protection against SQL injection. I would use one of the PDO drivers listed here (depending on your database) and follow the examples below that use prepared queries.

+3
source

it is still there (kinda), check zf2 / library / Zend / Db / Adapter / Platform /your DB adapter I found about 6 different quote() methods available in the MySql version . However, I think quoteValue() replaced the default quoteInto() , which we are all used to.

[ EDIT ]
Using the platform object will give some direction, but I think that most of the time the new SQL classes will use the api platform for citation for us. I still know this myself, so I'm not sure about anything.

-one
source

All Articles