我正在尝试使用 Controller 中的 Zend Debug 在 Zend Framework 中显示查询结果。但是 Model 中的“select”方法返回的唯一结果是这样的对象(仅包含结果数,但不包含数据结果):
object(Zend\Db\ResultSet\ResultSet)#303 (8) {
["allowedReturnTypes":protected] => array(2) {
[0] => string(11) "arrayobject"
[1] => string(5) "array"
}
["arrayObjectPrototype":protected] => object(Grupos\Model\CategoriaGrupo)#288 (6) {
["id"] => NULL
["titulo"] => NULL
["img"] => NULL
["alt"] => NULL
["slug"] => NULL
["ativo"] => NULL
}
["returnType":protected] => string(11) "arrayobject"
["buffer":protected] => NULL
["count":protected] => int(17)
["dataSource":protected] => object(Zend\Db\Adapter\Driver\Pdo\Result)#302 (8) {
["statementMode":protected] => string(7) "forward"
["resource":protected] => object(PDOStatement)#294 (1) {
["queryString"] => string(49) "SELECT `grupo_categoria`.* FROM `grupo_categoria`"
}
["options":protected] => NULL
["currentComplete":protected] => bool(false)
["currentData":protected] => NULL
["position":protected] => int(-1)
["generatedValue":protected] => string(1) "0"
["rowCount":protected] => int(17)
}
["fieldCount":protected] => int(6)
["position":protected] => int(0)
}
如何查看,不显示结果,只显示找到的行数:17。
所有其他查询都运行良好。表数据库和数据库权限都是正确的。我将在这里放置 Model.php、两个模型和 Controller 来向您展示:
模块.php
...
public function getServiceConfig()
{
return array(
'factories' => array(
'Grupos\Model\CategoriaGrupoTable' => function ($sm) {
$tableGateway = $sm->get('CategoriaGrupoTableGateway');
$table = new CategoriaGrupoTable($tableGateway);
return $table;
},
'CategoriaGrupoTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new CategoriaGrupo());
return new TableGateway('grupo_categoria', $dbAdapter, null, $resultSetPrototype);
}
)
);
}
GruposController.php
class GruposController extends AbstractActionController
{
protected $categoriaGrupoTable;
public function getCategoriaGrupoTable()
{
if (! $this->categoriaGrupoTable) {
$sm = $this->getServiceLocator();
$this->categoriaGrupoTable = $sm->get('Grupos\Model\CategoriaGrupoTable');
}
return $this->categoriaGrupoTable;
}
public function categoriasAction()
{
// Define as configurações e conteúdos das páginas
$tituloPagina = $translator->translate("Editar Grupo");
$categorias = $this->getCategoriaGrupoTable()->listar();
Debug::dump($categorias);
die();
}
}
模型 -> CategoryGrupoTable.php
<?php
namespace Grupos\Model;
use Zend\Db\TableGateway\TableGateway;
class CategoriaGrupoTable
{
protected $tableGateway;
function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
/**
* Método que lista todas as categorias ativas
*
* @return \Zend\Db\ResultSet\ResultSet
*/
public function listar()
{
return $this->tableGateway->select();
}
}
?>
模型 -> CategoryGrupo.php
<?php
namespace Grupos\Model;
class CategoriaGrupo
{
public $id;
public $titulo;
public $img;
public $alt;
public $slug;
public $ativo;
// Método que popula o objeto com dados vindos de uma array
public function exchangeArray($dados)
{
$this->id = (! empty($dados["id"])) ? $dados["id"] : null;
$this->titulo = (! empty($dados["titulo"])) ? $dados["titulo"] : "";
$this->img = (! empty($dados["img"])) ? $dados["img"] : "";
$this->alt = (! empty($dados["alt"])) ? $dados["alt"] : "";
$this->slug = (! empty($dados["slug"])) ? $dados["slug"] : "";
$this->titulo = (! empty($dados["ativo"])) ? 1 : 0;
}
// Esse método pegar o Objeto (que é a própria classe) e retornará ele como se fosse um array
public function getArrayCopy()
{
return get_object_vars($this);
}
}
?>
嗯..这就是问题所在,瞧源文件...如果有人能给我一盏灯,我将非常感激!我对我的英语感到抱歉。我尝试了两周才发现错误。谢谢。
最佳答案
我建议您阅读 zendframewrok 2 的文档,以更好地了解查询的工作原理。
现在来看看解决方案。下
模型 -> CategoryGrupoTable.php 修改 listar() 函数
public function listar()
{
$result = $this->tableGateway->select();
return $result->toArray();
}
关于php - ZF2 select 不显示任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288611/