2012-03-25 62 views
2

如何在使用时限制查询到特定列Zend_Db_Table_Abstract使用Zend_Db_Table_Abstract限制查询返回列

(getDbTable()下面返回Zend_Db_Table_Abstract对象)

$resultSet = $this->getDbTable()->fetchAll(
     $this->getDbTable()->select() 
     ->where('forgienKey = \'' . $forgienKey . '\'') 
     ->order("'id' ASC") 
    ); 

我只需要返回的ID列,但返回的整个行。谢谢你的帮助!

回答

3

正如the docs说:

$select = $table->select(); 
$select->from($table, array('bug_id', 'bug_description')) 
     ->where('bug_status = ?', 'NEW'); 

$rows = $table->fetchAll($select); 

所以,为您提供:

$resultSet = $this->getDbTable()->fetchAll(
     $this->getDbTable()->select() 
     ->from($this->getDbTable(), array('id')) 
     ->where('forgienKey = \'' . $forgienKey . '\'') 
     ->order("'id' ASC") 
); 
+0

这工作谢谢! – ktamlyn 2012-03-25 19:03:03

1

请试试这个

$resultSet = $this->getDbTable()->fetchAll(
     $this->getDbTable()->select() 
     ->columns('id') 
     ->where('forgienKey = \'' . $forgienKey . '\'') 
     ->order("'id' ASC") 
); 

编辑

检查链路

http://framework.zend.com/manual/en/zend.db.select.html

+0

没有去。这就是为什么我发布这个问题,因为这不起作用。 Zend手册指出你可以指定列,但没有说明如何。 – ktamlyn 2012-03-25 14:27:58

+1

在第3行有一个';',它不应该是 – Shikiryu 2012-03-25 14:42:40

+0

分号不是问题。您的答案看起来应该可以工作,但生成的查询会导致我的错误,因为使用 - > from的答案不会。我相信它是导致问题的Zend_Db_Table_Abstract的上下文。 – ktamlyn 2012-03-25 19:17:15