Zend_DB ResultSet Returns Empty

Hi

As i am trying to retrieve the records from postgresql database using ZF3 with Pdo adaptor.
But always am getting empty result but field count is shows right one and querystring also proper one,where am able to run directly on CLI.

Below mentioned one is my resultset.

Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)

[arrayObjectPrototype:protected] => Application\Model\Users Object
    (
        [id] => 
        [username] => 
    )

[returnType:protected] => arrayobject
[buffer:protected] => 
[count:protected] => 
[dataSource:protected] => Zend\Db\Adapter\Driver\Pdo\Result Object
    (
        [statementMode:protected] => forward
        [fetchMode:protected] => 2
        [resource:protected] => PDOStatement Object
            (
                [queryString] => SELECT "users".* FROM "users"
            )

        [options:protected] => 
        [currentComplete:protected] => 
        [currentData:protected] => 
        [position:protected] => -1
        [generatedValue:protected] => 
        [rowCount:protected] => 
    )

[fieldCount:protected] => 12
[position:protected] => 0

)

You need dump var_dump($resultSet->toArray()), not var_dump($resultSet) for see PDO fetch results

Hi

Am getting empty results . First of all am not able to use var_dump($resultSet->toArray()) . Getting another error while using toArray() as

“Rows as part of this DataSource, with type object cannot be cast to an array”.

And tried this var_dump((array)$resultSet) to see the output .

VarDump Output :

array(8) {
["*allowedReturnTypes"]=>
array(2) {
[0]=>
string(11) “arrayobject”
[1]=>
string(5) “array”
}
["*arrayObjectPrototype"]=>
object(Application\Model\Users)#149 (3) {
[“id”]=>
NULL
[“username”]=>
NULL
[“name”]=>
NULL
}
["*returnType"]=>
string(11) “arrayobject”
["*buffer"]=>
NULL
["*count"]=>
NULL
[“dataSource"]=>
object(Zend\Db\Adapter\Driver\Pdo\Result)#190 (9) {
[“statementMode”:protected]=>
string(7) “forward”
[“fetchMode”:protected]=>
int(2)
[“resource”:protected]=>
object(PDOStatement)#189 (1) {
[“queryString”]=>
string(29) "SELECT “users”.
FROM “users””
}
[“options”:protected]=>
NULL
[“currentComplete”:protected]=>
bool(false)
[“currentData”:protected]=>
NULL
[“position”:protected]=>
int(-1)
[“generatedValue”:protected]=>
NULL
[“rowCount”:protected]=>
NULL
}
["*fieldCount"]=>
int(12)
["*position"]=>
int(0)
}

This is a pdo_sqlite usage simple code. Hope it can help you.


use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

include __DIR__ . '/vendor/autoload.php';

$db = new \Zend\Db\Adapter\Adapter([
    'driver' => 'Pdo_Sqlite',
    'database' => '/tmp/users.db'
]);

$db->query('DROP TABLE IF EXISTS users')->execute();
$db->query('CREATE TABLE users (
    user_id INTEGER PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL
);')->execute();

$resultSet = new ResultSet();
$resultSet->setArrayObjectPrototype(new UserEntity);
$table = new TableGateway('users', $db, null, $resultSet);

$table->insert(['user_id' => 1, 'username' => 'test1', 'password' => '123']);

$resultSet = $table->select();
//var_dump($resultSet);

echo $resultSet->count(), "\n";
// Output: 1

print_r($resultSet->toArray());
// Output: [ ["user_id" => 1, "username" => "test1", "password" => "123"] ]

class UserEntity extends ArrayObject {
}

Hi
It’s working fine. And i hope my problem in not converting to array as i think.

When am trying to call toArray() on resultSet Object then am getting below error. Will you

"Rows as part of this DataSource, with type object cannot be cast to an array"

Yeah, the problem is you ResultSet::arrayObjectPrototype (Application\Model\Users).

It’s must be extend ArrayObject, or implement toArray or getArrayCopy method, if you are use the default Zend\Db\ResultSet\ResultSet.

Second way. You can try use the Zend\Db\ResultSet\HydratingResultSet. This is HydratingResultSet simple code block.

$resultSet = new \Zend\Db\ResultSet\HydratingResultSet(new \Zend\Hydrator\ClassMethods, new UserEntity);

class UserEntity {
    protected $userId;
    protected $username;
    protected $password;

    public function getUserId(){ return $this->userId; }
    public function setUserId($userId){ $this->userId = $userId; }
    
    public function getUsername(){ return $this->username; }
    public function setUsername($username) { $this->username = $username; }

    public function getPassword() { return $this->password; }
    public function setPassword($password) { $this->password = $password; }

}

Hi

As i trying to extend Class with ArrayObject or trying to use HydratingResultSet. But both cases am getting Internal Server Error without any error in detail.

Even already enabled all php_reporting as well.

Let me know how to enable debugging at first to identify the issue.

I wrote a blogpost about using direct ArrayObject instance as object prototype in zend-db, hope it usefull https://samsonasik.wordpress.com/2017/05/25/using-direct-arrayobject-instance-as-objectprototype-in-zenddb/