How can disable quote join Zend db

不问归期 提交于 2019-12-13 04:01:01

问题


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.



来源:https://stackoverflow.com/questions/15940765/how-can-disable-quote-join-zend-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!