2009-08-24 12 views
10

निम्न पंक्ति की सरणी। मैं इसके बजाय एक एसोसिएटिव सरणी प्राप्त करना चाहता हूं।Zend_Db_Table साहचर्य इसके बजाय वस्तु

मुझे पता है कि Zend_Db के लिए fetchAssoc() विधि है लेकिन Zend_Db_Table में कुछ भी समान है (मैंने fetchAssoc() की कोशिश की लेकिन यह काम नहीं करता है, मुझे दस्तावेज़ में कुछ भी नहीं मिला है)?

उत्तर

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

दोनों Zend_Db_Table_Row और Zend_Db_Table_Rowset एक toArray() विधि है। एक पंक्ति को एक सहयोगी सरणी के रूप में वापस किया जाता है, और एक रोसेट को सहयोगी सरणी के एक साधारण (क्रमिक) सरणी के रूप में वापस कर दिया जाता है।

2

विधेयक के जवाब आगे करने के लिए, यदि आप चाहते थे rowset एक साहचर्य सरणी (बल्कि क्रमसूचक से) एकमात्र विकल्प 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()); 
संबंधित मुद्दे