10

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)?

3 Answers 3

19
$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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.