How can disable quote join Zend db

2019-08-31 02:16发布

问题:

I've sql query

    select * from table1
    left join (values (4),(1800),(103500)) AS "filter (id) on table1.id=filter.id

By default Zend_Db_Select table quoted. For example:

    $result = '(values (4),(1800),(103500)) AS filter (id)';
    $select->joinInner($result, "table1.id = filter.id", '');

result:

    SELECT * FROM "table1"
    INNER JOIN "(values (4),(1800),(103500)) filter (id)" ON table1.id=filter.id

Me need

  SELECT * FROM "table1"
    INNER JOIN (values (4),(1800),(103500)) filter (id) ON table1.id=filter.id

How can disable quote table?

回答1:

This is a little tricky. Look at the code below.

$dbh = Zend_Db_Table::getDefaultAdapter();
$select = $dbh->select();
$select->from('table1');
$select->joinInner(
        array('filter (id)' => new Zend_Db_Expr('(values (4),(1800),(103500))')),
        "table1.id = filter.id",
        array()
);
echo $select->assemble() . PHP_EOL;

This code by default outputs the following statement which is not what we really want because identifier filter (id) is quoted. Here is the output.

SELECT `table1`.* FROM `table1`
 INNER JOIN (values (4),(1800),(103500)) AS `filter (id)` ON table1.id = filter.id

We need to disable autoQuoteIdentifiers in configuration options. For example:

    'db' => array(
        'adapter' => 'pdo_mysql',
        'isDefaultTableAdapter' => true,
        'params' => array(
            'host' => '<host>',
            'username' => '<user>',
            'password' => '<pass>',
            'dbname' => '<db>',
            'options' => array(
                'autoQuoteIdentifiers' => false,
            ),
        ),
    )

We get the following output

SELECT table1.* FROM table1
 INNER JOIN (values (4),(1800),(103500)) AS filter (id) ON table1.id = filter.id

Note that in this case developer is responsible for quoting the identifiers when needed.

I think it's impossible to selectively disable quoting for one of the table alias. Well at least I found this impossible when reviewed 1.x Zend Framework code I have here locally ;)



回答2:

Try adding $result to your $select as a Zend_Db_Expr.