我想生成以下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的例子并没有对此有所了解。
答案 0 :(得分:1)
你真是太近了! join()
实际上有一个第3个参数,您可以在其中提供列名称,就像from()
中的第二个参数一样。
这意味着->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name'))
应该生成您正在寻找的SQL。
join()的第三个参数是一个列名数组,就像from()方法中使用的那样。它默认为“*”,支持相关名,表达式和Zend_Db_Expr,方法与from()方法中的列名数组相同。