我正在使用 zend framework3 并为数据库使用 Mysql 驱动程序。我正在使用下面的代码来获取数据:
$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 中工作正常。感谢任何人的任何帮助。
我尝试了很多,发现如果替换代码
$selectString = $this->sql->getSqlStringForSqlObject($select);
$results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);
与
$statement = $this->sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
然后我得到了结果。但我的问题是为什么我没有通过
$selectString = $this->sql->getSqlStringForSqlObject($select);
$results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);