1

我正在使用 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);
4

1 回答 1

1

getSqlStringForSqlObject自 2.4 版起已弃用。但是我不知道这是否是它不再起作用的原因。

根据文档,有两种方法。使用准备语句,您使用的语句完全相同:

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

通过执行查询,您可能正在寻找:

$selectString = $sql->buildSqlString($select);
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
于 2016-11-10T09:08:05.870 回答