ZF2调试结果集计数返回null

时间:2016-11-17 01:13:15

标签: php zend-framework zend-framework2

当我尝试调试ResultSet时,我在ZF2中遇到问题我一直都有["count":protected] => NULL。我在try/catch事件上使用onBootstrap()检查数据库连接,并且连接正常。另外,我检查我的工厂类的依赖性,一切都好!在数据库中,表posts中有5个项目,但计数为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)
}

映射器:

  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',
        ),
    ),
);

1 个答案:

答案 0 :(得分:0)

在您调用count方法之前,属性$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/

中的更多信息