如何禁用引用连接 Zend db [英] How can disable quote join Zend db
问题描述
我有sql查询
select * from table1
left join (values (4),(1800),(103500)) AS "filter (id) on table1.id=filter.id
默认 Zend_Db_Select 表被引用.例如:
By default Zend_Db_Select table quoted. For example:
$result = '(values (4),(1800),(103500)) AS filter (id)';
$select->joinInner($result, "table1.id = filter.id", '');
结果:
SELECT * FROM "table1"
INNER JOIN "(values (4),(1800),(103500)) filter (id)" ON table1.id=filter.id
我需要
SELECT * FROM "table1"
INNER JOIN (values (4),(1800),(103500)) filter (id) ON table1.id=filter.id
如何禁用报价表?
推荐答案
这有点棘手.看看下面的代码.
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;
此代码默认输出以下语句,这不是我们真正想要的,因为标识符 filter (id)
被引用.这是输出.
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
我们需要在配置选项中禁用 autoQuoteIdentifiers
.例如:
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,
),
),
)
我们得到以下输出
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.
我认为不可能有选择地禁用表别名之一的引用.好吧,至少我在查看本地的 1.x Zend Framework 代码时发现这是不可能的;)
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 ;)
这篇关于如何禁用引用连接 Zend db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!