Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Db_Table_Rowset to array of objects

I am not looking for $myRowset->toArray(); because I want to get an array of objects.

What I want to do is to be able to merge $myRowset with an array of objects.

$myOtherArray = [new Foo(), new Bar()];
$array = array_merge($myRowset, $myOtherArray);

With a Zend_Db_Table_Rowset, it is impossible. Using $myRowset->toArray(); doesn't work too because I need an array of objects.

Edit - Example of code doing what I want, but I'm looking for a better solution if it exists:

// Convert the Zend_Db_Table_Rowset to an array of Zend_Db_Table_Row
$myRowset = $dbTable->fetchAll();
$rowArray = array();
foreach ($myRowset as $row) {
    $rowArray[] = $row;
}

// Merge with other array of objects
$myOtherArray = [new Foo(), new Bar()];

$finalArray = array_merge($rowArray, $myOtherArray);
like image 214
Matthieu Napoli Avatar asked Mar 11 '26 13:03

Matthieu Napoli


1 Answers

Here is my "cleanest" solution: I override Zend_Db_Table_Rowset

class My_RowSet extends Zend_Db_Table_Rowset {

    public function rowArray() {
        $rowArray = array();
        foreach ($this as $row) {
            $rowArray[] = $row;
        }
        return $rowArray;
    }

}

and Zend_Db_Table:

abstract class My_Db_Table extends Zend_Db_Table {

    protected $_rowsetClass = 'My_RowSet';

}

Now I can do:

$myRowset = $dbTable->fetchAll();
$rowArray = $myRowset->rowArray();
like image 176
Matthieu Napoli Avatar answered Mar 14 '26 01:03

Matthieu Napoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!