2016-11-09 122 views
1

我正在使用zend framework3並使用數據庫的Mysql驅動程序。 我使用下面的代碼來獲取數據:無法從zf3中的ResultSet對象數組中獲取數據

 $con = $this->adapter; 
     $select = $this->sql->select(); 
     $select->from('nav_menu'); 
     $selectString = $this->sql->getSqlStringForSqlObject($select); 
     $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE); 
     $resultSet = new ResultSet(); 
     $resultSet->initialize($results); 

當我的var_dump這個數據我得到如下結果:

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

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

     [arrayObjectPrototype:protected] => ArrayObject Object 
      (
       [storage:ArrayObject:private] => Array 
        (
        ) 

      ) 

     [returnType:protected] => arrayobject 
     [buffer:protected] => -1 
     [count:protected] => 
     [dataSource:protected] => Zend\Db\Adapter\Driver\Mysqli\Result Object 
      (
       [resource:protected] => mysqli_result Object 
        (
         [current_field] => 0 
         [field_count] => 8 
         [lengths] => 
         [num_rows] => 15 
         [type] => 0 
        ) 

       [isBuffered:protected] => 1 
       [position:protected] => 0 
       [numberOfRows:protected] => -1 
       [currentComplete:protected] => 
       [nextComplete:protected] => 
       [currentData:protected] => 
       [statementBindValues:protected] => Array 
        (
         [keys] => 
         [values] => Array 
          (
          ) 

        ) 

       [generatedValue:protected] => 0 
      ) 

     [fieldCount:protected] => 8 
     [position:protected] => 0 
    ) 

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

我得到一個錯誤,當我這個迭代來得到的值如下:

 foreach ($resultSet as $key => $value) { 
      $array[$i]['id'] = $value->id; 
      $array[$i]['name'] = $value->name; 
      $array[$i]['label'] = $value->label; 
      $array[$i]['route'] = $value->route; 
      $array[$i]['parent_id'] = $value->parent_id; 
      $i++; 
     } 

我不是在哪裏我錯了。雖然有num_rows是15.這是在ZF2罰款。 任何人的任何幫助表示讚賞。

我嘗試了很多,發現如果與

$statement = $this->sql->prepareStatementForSqlObject($select); 
    $results = $statement->execute(); 

替換代碼

$selectString = $this->sql->getSqlStringForSqlObject($select); 
    $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE); 

然後我得到的result.But我的問題是,爲什麼我沒有通過

得到結果
$selectString = $this->sql->getSqlStringForSqlObject($select); 
    $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE); 

回答

1

getSqlStringForSqlObject從版本2.4開始已棄用。但我不知道這是否是它不再工作的原因。

根據docs有2種方法。隨着準備語句,它使用的是完全相同的:

$statement = $sql->prepareStatementForSqlObject($select); 
$results = $statement->execute(); 

與執行查詢,這你可能尋找:

$selectString = $sql->buildSqlString($select); 
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE); 
+0

thanx的clearification。 –

+0

你有什麼想法如何調用程序。我正在使用此代碼 $ results = $ con> query('CALL'fetching_menu'(?)',$ spParams); \t \t $ resultSet = new ResultSet; \t \t $ resultSet-> initialize($ results); 其中$ spParams = array('backend'); 我收到一個錯誤。 –

+0

我解決了這個問題。我用 $ spParams = ['backend']; \t \t $ statement = $ con> createStatement('CALL'fetching_menu'(?)',$ spParams); \t \t \t \t $ result = $ statement-> execute(); \t \t \t \t $ resultSet = new ResultSet; \t \t \t $ resultSet-> initialize($ result); –