• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm simply trying to fetch all records in a given table by extending Zend AbstractTableGateway and making use of inherited select() function. this select() function returns type Zend ResultSet however I'm not able get an array of results using toArray().

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 }

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;
}
  • 6
Reply Report

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;

  • 4
Reply Report

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);
}
  • 2
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions