Reputation: 83697
The following line:
$select = $table->select();
$select->where('approved = 1');
$result = $table->fetchRow($select);
Returns an object. What I would like is to get an associative array instead.
I know that Zend_Db has fetchAssoc() method for that but is something similar also in the Zend_Db_Table (I tried fetchAssoc() but it doesn't work, I haven't found anything in the documentation)?
Upvotes: 10
Views: 5310
Reputation: 11
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);
}
}
Then in your code:
$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());
Upvotes: 1
Reputation: 9078
To further Bill's answer, if you wanted the Rowset returned as an associative array (rather than ordinal) the only choice appears to be Zend_Db (as you alluded to):
$db = $table->getAdapter();
$select = $table->select();
$select->where('approved = 1');
$result = $db->fetchAssoc($select);
Upvotes: 2
Reputation: 562260
$result = $table->fetchRow($select)->toArray();
Both Zend_Db_Table_Row
and Zend_Db_Table_Rowset
have a toArray()
method. A Row is returned as an associative array, and a Rowset is returned as a simple (ordinal) array of associative arrays.
Upvotes: 19