2016-11-17 4 views
1

У меня проблема в ZF2, когда я пытаюсь отлаживать ResultSet i все время есть ["count":protected] => NULL. Я проверяю соединение db с помощью try/catch по адресу onBootstrap(). Событие и соединение в порядке. Также я проверяю свой заводский класс на зависимость, и есть все в порядке! В базе данных у меня есть 5 элементов в таблице posts, но в графе NULL. Что может быть проблемой?ZF2 debug result set count return null

object(Zend\Db\ResultSet\ResultSet)#161 (8) { 
    ["allowedReturnTypes":protected] => array(2) { 
    [0] => string(11) "arrayobject" 
    [1] => string(5) "array" 
    } 
    ["arrayObjectPrototype":protected] => object(ArrayObject)#188 (1) { 
    ["storage":"ArrayObject":private] => array(0) { 
    } 
    } 
    ["returnType":protected] => string(11) "arrayobject" 
    ["buffer":protected] => NULL 
    ["count":protected] => NULL 
    ["dataSource":protected] => object(Zend\Db\Adapter\Driver\Pdo\Result)#160 (9) { 
    ["statementMode":protected] => string(7) "forward" 
    ["fetchMode":protected] => int(2) 
    ["resource":protected] => object(PDOStatement)#162 (1) { 
     ["queryString"] => string(29) "SELECT `posts`.* FROM `posts`" 
    } 
    ["options":protected] => NULL 
    ["currentComplete":protected] => bool(false) 
    ["currentData":protected] => NULL 
    ["position":protected] => int(-1) 
    ["generatedValue":protected] => string(1) "0" 
    ["rowCount":protected] => NULL 
    } 
    ["fieldCount":protected] => int(3) 
    ["position":protected] => int(0) 
} 

Mapper:

public function __construct(AdapterInterface $adapterInterface) 
    { 
     $this->dbAdapter = $adapterInterface; 
    } 

    public function findAllPosts() 
    { 
     $sql = new Sql($this->dbAdapter); 
     $select = $sql->select('posts'); 

     $stmt = $sql->prepareStatementForSqlObject($select); 
     $result = $stmt->execute(); 

     if ($result instanceof ResultInterface && $result->isQueryResult()) { 
      $resultSet = new ResultSet(); 

      \Zend\Debug\Debug::dump($resultSet->initialize($result));die(); 
     } 

     die("no data"); 
    } 

Фабрика:

class SqlPostMapperFactory 
{ 
    /** 
    * @param ServiceLocatorInterface $serviceLocatorInterface 
    * @return SqlPostMapper 
    */ 
    public function __invoke(ServiceLocatorInterface $serviceLocatorInterface) 
    { 
     return new SqlPostMapper(
      $serviceLocatorInterface->get('Zend\Db\Adapter\Adapter') 
     ); 
    } 
} 

local.php

return array(
    'db' => array(
     'driver'   => 'Pdo', 
     'dsn'   => 'mysql:dbname=zf2;host=localhost', 
     'username'  => 'root', 
     'password'  => '', 
    ), 
    'service_manager' => array(
     'factories' => array(
      'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', 
     ), 
    ), 
); 

ответ

0

Атрибут count есть null до вызывать $resultSet->count() способ.

Проверить выполнение в AbstractResultSet.php:

public function count() 
{ 
    if ($this->count !== null) { 
     return $this->count; 
    } 
    if ($this->dataSource instanceof Countable) { 
     $this->count = count($this->dataSource); 
    } 
    return $this->count; 
} 

Вы можете перебора результирующего набора и доступа эти столбцы:

use Zend\Db\Adapter\Driver\ResultInterface; 
use Zend\Db\ResultSet\ResultSet; 

$statement = $driver->createStatement('SELECT * FROM users'); 
$statement->prepare(); 
$result = $statement->execute($parameters); 

if ($result instanceof ResultInterface && $result->isQueryResult()) { 
    $resultSet = new ResultSet; 
    $resultSet->initialize($result); 

    foreach ($resultSet as $row) { 
     echo $row->my_column . PHP_EOL; 
    } 
} 

Более подробная информация в https://zendframework.github.io/zend-db/