Zend Framework:我如何“自动”构建正确生成 SQL 查询
我想生成以下 SQL:
SELECT `rc`.*, `c`.`name` FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
但是我得到:
SELECT `rc`.*, `c`.* FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
区别是我想要 c.name
,而不是 c.*
使用以下 ZF PHP 代码:
public function fetchConfigurations($clientId = null, $order = 'rc.config_name ASC')
{
$db = $this->getDb();
$stmt = $db->select()
->from(array('rc' => 'RunConfigurations','c.name'))
->join(array('c' => 'Clients'),'rc.client_id = c.id')
->order($order);
if(is_numeric($clientId))
{
$stmt->where('rc.client_id = ?')
->bind(array($clientId));
}
$results = $db->fetchAll($stmt);
if(sizeof($results) > 0)
{
$configs = array();
foreach($results as $row)
{
$configs[] = $this->createRunConfigurationFromRow($row);
}
return $configs;
}
else
{
die($stmt->__toString());
return null;
}
}
这很令人恼火,我觉得我在以下任一处遗漏了一些东西:
->from(array('rc' => 'RunConfigurations','c.name'))
或
->join(array('c' => 'Clients'),'rc.client_id = c.id')
ZF 示例没有对此提供任何说明。
I want to generate the following SQL:
SELECT `rc`.*, `c`.`name` FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
However I am getting:
SELECT `rc`.*, `c`.* FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
The difference is I want c.name
, not c.*
Using the following ZF PHP code:
public function fetchConfigurations($clientId = null, $order = 'rc.config_name ASC')
{
$db = $this->getDb();
$stmt = $db->select()
->from(array('rc' => 'RunConfigurations','c.name'))
->join(array('c' => 'Clients'),'rc.client_id = c.id')
->order($order);
if(is_numeric($clientId))
{
$stmt->where('rc.client_id = ?')
->bind(array($clientId));
}
$results = $db->fetchAll($stmt);
if(sizeof($results) > 0)
{
$configs = array();
foreach($results as $row)
{
$configs[] = $this->createRunConfigurationFromRow($row);
}
return $configs;
}
else
{
die($stmt->__toString());
return null;
}
}
This is aggravating and I feel like I am missing something at either:
->from(array('rc' => 'RunConfigurations','c.name'))
or
->join(array('c' => 'Clients'),'rc.client_id = c.id')
and the ZF examples are not shedding any light on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你太近了!
join()
实际上有一个第三个参数,您可以在其中提供列名称,就像from()
中的第二个参数一样。这意味着
->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name'))
应该生成 SQL你正在寻找。-- 引用自 Zend 框架手册:
You are so close!
join()
actually has a 3rd parameter in which you can supply the column names just like the 2nd parameter fromfrom()
.This would mean that
->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name'))
should generate the SQL you are looking for.-- Quote from the Zend Framework manual: