Php ZF2调试结果集计数返回null
当我尝试调试Php ZF2调试结果集计数返回null,php,zend-framework,zend-framework2,Php,Zend Framework,Zend Framework2,当我尝试调试ResultSet时,ZF2出现问题,我一直都有[“count”:protected]=>NULL。我使用onBootstrap()事件上的try/catch检查数据库连接,连接正常。我还检查了我的工厂类的依赖性,一切都正常!在数据库中,我在表posts中有5项,但在计数中为NULL。有什么问题吗 object(Zend\Db\ResultSet\ResultSet)#161 (8) { ["allowedReturnTypes":protected] => array(2
ResultSet
时,ZF2出现问题,我一直都有[“count”:protected]=>NULL
。我使用onBootstrap()
事件上的try/catch
检查数据库连接,连接正常。我还检查了我的工厂类的依赖性,一切都正常!在数据库中,我在表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')
);
}
}
工厂:
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',
),
),
);
在调用
$resultSet->count()
方法之前,属性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;
}
}
中的详细信息在调用
$resultSet->count()
方法之前,属性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;
}
}
更多信息请访问