简体   繁体   中英

ZF2 - \Zend\Db\Adapter\Platform::getQuoteIdentifierSymbol()

Code is as following, where I aim to use Pdo_mysql:

use \Zend\Db\Adapter\Adapter;
use \Zend\Db\Sql\Sql;
use \Zend\Db\Sql\Expression;    

$params = array(
    'driver'   => "Pdo_mysql",
    'host'     => &$this->Registry->config[ 'sql' ][ 'host' ],
    'username' => &$this->Registry->config[ 'sql' ][ 'user' ],
    'password' => &$this->Registry->config[ 'sql' ][ 'passwd' ],
    'dbname'   => &$this->Registry->config[ 'sql' ][ 'dbname' ]
);
$this->adapter  = new \Zend\Db\Adapter\Adapter( $params );
$this->platform = $this->adapter->getPlatform();
$this->sql      = new Sql( $this->adapter );

And when I check identifier-quote symbol with:

print $this->platform->getQuoteIdentifierSymbol();    // Output: "

As you can see, double quote is the symbol. This of course invalidates all MySQL queries of mine, since it quotes all identifier names (tables, columns etc) with double-quotes (") instead of forward-quote (`).

So, why PDO-MySQL driver uses Sql92 symbol instead? And how to fix that?

The only solution to this problem was to create new \\Zend\\Db\\Platform\\Mysql object and pass it as second parameter to Adapter class when I initiate a connection. This question implies the automatic way of doing it. Why initializing MySQL adapter doesn't attach MySQL platform to it, is something I don't understand - instead it attaches SQL92 platform.

Check the function setConnectionParameters() in Zend\\Db\\Adapter\\Driver\\Pdo\\Connection

if $params['dsn'] is set the platform is taken from the text before ':'
Example:

$params['dsn'] = mysql:dbname=database;host=db.host.com
Platform name will be 'mysql'

Else, if $params['pdodriver'] is set the platform will be that value
Example:

$params['pdodriver'] = 'mysql'
Platform name will be 'mysql'

Else, if $params['driver'] is set the platform is taken from the text after 'Pdo_'
Example:

$params['driver'] = 'Pdo_Mysql'
Platform name will be 'mysql'

The platform name will decide the connection type an thus the correct quote identifier symbols

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM