php - ZF2 select 不显示任何结果

标签 php mysql debugging select zend-framework

我正在尝试使用 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/

相关文章:

c++ - 给定一个包含 lambda 函数的调用堆栈,如何确定其来源?

javascript - 从单击的按钮运行外部 javascript 函数,该按钮是在 WordPress 中使用 jquery 动态创建的

PHP 登录脚本导致空白页

mysql - MySQL 中的 "CACHE INDEX"和 "LOAD INDEX INTO CACHE"

Android studio 调试器无法打开选定的 VM 调试端口 (8700)

css - Chrome 11 解析 CSS 规则的错误

php - MYSQL搜索字段中的特定单词或数字,不包括包含这些单词或数字的单词或数字

php - 如何对多个表使用带有 group by 的连接语法

MySQL评论表结构问题

php - 立即显示数据库中的少量条目的最佳选择?