2016-11-17 10 views
1

私はいつもをデバッグしようとするとZF2に問題があります。["count":protected] => NULLがあります。私はtry/catchonBootstrap()で使用してdb接続をチェックし、接続は問題ありません。また、私は依存のために私の工場クラスをチェックし、すべてがOKです!データベースでは、テーブルpostsに5つの項目がありますが、数はNULLです。何が問題なのでしょうか?ZF2デバッグ結果セットカウントが返される

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

答えて

0

メソッドを呼び出すまで、countという属性はnullです。

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; 
} 

あなたは結果セットを反復処理し、これらの列にアクセスすることができます。https://zendframework.github.io/zend-db/

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; 
    } 
} 

さらに詳しい情報

関連する問題