It's not a documented feature, but you can instantiate a Rowset object with any arbitrary array of arrays of data. This is what Zend_Db_Table does in its find()
and fetchall()
methods.
<?php
// this can be the result of any Zend_Db_Statement::fetchAll()
$query_result = array(
array('id'=>123, 'foo'=>'bar'),
array('id'=>456, 'foo'=>'baz')
);
$rowset = new Zend_Db_Table_Rowset(
array(
'readonly' => true,
'data' => $query_result
)
);
foreach ($rowset as $row) {
print "id = " . $row->id . "\n";
print "foo = " . $row->foo . "\n";
}
Note that this is not a "live" rowset. You can't change fields and save()
them to the base tables in the database. There's no information stored in the rowset or rows about which base tables contain the columns corresponding to the fields of your Row objects. Some columns of your original SQL query may be expressions or aggregations, so there would be no deterministic way to save()
them anyway.
So this is basically no improvement over using PDO::FETCH_OBJ
to fetch result sets as objects instead of hash arrays.
Re comments:
Yes, you can use setTable()
just like with a deserialized Rowset object. But Zend_Db_Table_Row does not have any idea which table each column belongs to. So it will try to generate an UPDATE or INSERT query based on the fields of the Row object. This will fail if the result set from which you make your Row object includes columns from more than one joined tables.
It's up to you to make sure the Row object contains fields only from a single table, and that the primary key column(s) are included, and that the Row is unambiguously mapped from a single row in the table.