2010-03-12 82 views
1

我想弄清楚如何正確使用Zend_Db_Table_Abstract。我想僅返回查詢中的name列。你能解釋下面的代碼有什麼問題嗎?Zend_Db_Table_Select是如何工作的?

class Model_DbTable_Foo extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'foo'; 

    public function getFooById($id) { 
    $select = $this->select(true)->columns('name')->where('id=' . $id); 
    $row = $this->fetchRow($select); 
    print_r($row->toArray()); 
    } 
} 

更新:

從下面@Joshua史密斯的例子,我能弄清楚如何使用select()來正確地做到這一點:

$select = $this->select() 
    ->from($this->_name, 'name') // The 2nd param here could be an array. 
    ->where('id = ?', $id); 
$row = $this->fetchRow($select); 
print_r($row->toArray()); 

回答

3

您的代碼非常接近工作:

class Model_DbTable_Foo extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'foo'; 

    public function getFooById($id) { 
    $row = $this->find($id)->current(); 
    return $row->name; 
    } 
} 

http://framework.zend.com/manual/en/zend.db.table.html 有關使用find的更多信息,請參見示例#25以瞭解特定列選擇和「按主鍵查找行」。

+0

謝謝!用例#25我能弄清楚select()是如何工作的。我會在上面發佈我的代碼。 – jwhat 2010-03-12 13:07:31