2009-08-24 23 views
10

Các dòng sau:Zend_Db_Table - Associative mảng thay vì Object

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

Trả về một đối tượng. Những gì tôi muốn là để có được một mảng kết hợp thay thế.

Tôi biết rằng Zend_Db có phương thức fetchAssoc() nhưng cũng giống như trong Zend_Db_Table (tôi đã thử fetchAssoc() nhưng nó không hoạt động, tôi không tìm thấy gì trong tài liệu)?

Trả lời

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

Cả hai Zend_Db_Table_RowZend_Db_Table_Rowset có phương thức toArray(). Một hàng được trả về như một mảng kết hợp, và một Rowset được trả về như một mảng đơn giản (thứ tự) của các mảng kết hợp.

2

Tiếp tục câu trả lời của Bill, nếu bạn muốn rowSet trả về là một mảng kết hợp (chứ không phải là thứ tự) các lựa chọn duy nhất dường như là Zend_Db (như bạn ám chỉ):

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

Sau đó, trong mã của bạn:

$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()); 
Các vấn đề liên quan