2009-08-24 85 views
10

下面的行的:的Zend_Db_Table - 關聯數組代替對象

$select = $table->select(); 
$select->where('approved = 1'); 
$result = $table->fetchRow($select); 

返回對象。我想要的是取而代之的是一個關聯數組。我知道Zend_Db具有fetchAssoc()方法,但在Zend_Db_Table中也是類似的(我試過fetchAssoc()但它不起作用,我在文檔中沒有找到任何東西)?

回答

18
$result = $table->fetchRow($select)->toArray(); 

兩個Zend_Db_Table_RowZend_Db_Table_Rowset具有toArray()方法。 Row作爲關聯數組返回,Rowset作爲關聯數組的簡單(有序)數組返回。

2

爲了進一步比爾的回答,如果你想要的行集返回一個關聯數組(不是序號)唯一的選擇似乎是Zend_Db的(如你所提到的):在

$db  = $table->getAdapter(); 
$select = $table->select(); 
$select->where('approved = 1'); 
$result = $db->fetchAssoc($select); 
1
Zend_Loader::loadClass('Zend_Db_Table'); 
class SomeTable extends Zend_Db_Table_Abstract{ 

protected $_name = 'sometable'; 

public function getAssoc($where = null, $order = null, $count = null, $offset = null){ 
    if (!($where instanceof Zend_Db_Table_Select)) { 
     $select = $this->select(); 

     if ($where !== null) { 
      $this->_where($select, $where); 
     } 

     if ($order !== null) { 
      $this->_order($select, $order); 
     } 

     if ($count !== null || $offset !== null) { 
      $select->limit($count, $offset); 
     } 

    } else { 
     $select = $where; 
    } 
    return $this->getAdapter()->fetchAssoc($select);   
} 
} 

然後您的代碼:

$this->some_table = new SomeTable(); 
//Get and print some row(s) 
$where = $this->some_table->getAdapter()->quoteInto('primarykey_name = ?', $primarykey_value); 
print_r($this->somes_table->getAssoc($where)); 

//Get and print all rows 
print_r($this->areas_table->getAssoc());