2009-08-24 10 views
10

La siguiente línea:Zend_Db_Table - asociativa matriz en lugar de objetos

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

Devuelve un objeto. Lo que me gustaría es obtener una matriz asociativa en su lugar.

Sé que Zend_Db tiene método fetchAssoc() para eso pero es algo similar también en Zend_Db_Table (probé fetchAssoc() pero no funciona, no he encontrado nada en la documentación)?

Respuesta

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

Tanto Zend_Db_Table_Row y Zend_Db_Table_Rowset tienen un método toArray(). Una fila se devuelve como una matriz asociativa, y un conjunto de filas se devuelve como una matriz simple (ordinal) de matrices asociativas.

2

Para aún más la respuesta de Bill, si desea el conjunto de filas devuelve como una matriz asociativa (en lugar de ordinales) la única opción parece ser Zend_Db (como se hizo alusión a):

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

Luego, en su código:

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