Zend framework 2 \Zend\Db\ResultSet\ResultSet->toArray() doe not return records
Solution 1
You should use HydratingResultSet
like this :
class MyClassTable extends AbstractTableGateway
{
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
$this->resultSetPrototype = new HydratingResultSet();
$this->resultSetPrototype->setObjectPrototype(new MyClass());
$this->initialize();
}
public function fetchAll()
{
$resultSet = $this->select();
return $resultSet;
}
public function fetchAllToArray()
{
$aData = $this->fetchAll()->toArray();
return $aData;
}
Solution 2
You can also try this
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('table');
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($results);
print_r($resultSet->toArray());
With Zend\Db\ResultSet\ResultSet;
Solution 3
Just try to use
(array)$resultSet
I've used this sometimes on ZF and works fine.
Solution 4
Mine issue was as @Fatmuemoo noted.
If you register your custom object prototype, code eg.
$resultSetPrototype = new ResultSet($entityClassName, new $entityClassName);
$instance->setResultSetPrototype($resultSetPrototype);
you have to implement toArray() method in yout Entity class.
public function toArray()
{
return get_object_vars($this);
}
Related videos on Youtube
akond
I'm a Clojure/ClojureScript, JS, PHP and a little bit of R/Go programmer. Hire me at https://www.linkedin.com/in/toropenko/
Updated on June 04, 2022Comments
-
akond almost 2 years
I'm simply trying to fetch all records in a given table by extending Zend
AbstractTableGateway
and making use of inheritedselect()
function. thisselect()
function returns type ZendResultSet
however I'm not able get an array of results usingtoArray()
.I get the following message:
Rows as part of this DataSource, with type object cannot be cast to an array
Update
I worked it out
assuming you have extended AbstractTableGateway
$resultSet = $this->select(); foreach($resultSet as $row) { echo $row->yourProperty }
-
VijayZend_Db_Result is already having a toArray(). could you please paste your code for reference.framework.zend.com/apidoc/2.0/classes/…
-
FatmuemooFYI: if you are using a custom array object prototype in the result set, adding a toArray() method to your entity will fix this
-
-
NaN about 10 yearsAwesome shortcut. This has saved me a ton of time.