2009-08-24 9 views
10

la seguente riga:Zend_Db_Table - Associative Array Invece dell'oggetto

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

restituisce un oggetto. Quello che vorrei è ottenere un array associativo.

So che Zend_Db ha il metodo fetchAssoc() per quello ma è qualcosa di simile anche in Zend_Db_Table (ho provato fetchAssoc() ma non funziona, non ho trovato nulla nella documentazione)?

risposta

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

Sia Zend_Db_Table_Row e Zend_Db_Table_Rowset hanno un metodo di toArray(). Una riga viene restituita come array associativo e un set di righe viene restituito come una semplice matrice (ordinale) di array associativi.

2

ad ulteriori risposta di Bill, se si voleva il set di righe restituito come un array associativo (piuttosto che ordinale) l'unica scelta sembra essere Zend_Db (come si allude):

$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);   
} 
} 

Poi, nel il tuo codice:

$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()); 
Problemi correlati