ZF2 select 没有显示任何结果

ZF2 select not shows any results

我尝试在控制器中使用 Zend Debug 在 Zend Framework 中显示查询结果。但是Modelreturns中的"select"方法唯一的就是这样一个对象(只有结果数,没有de数据结果):

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。

所有其他查询都完美无缺。 table 数据库和数据库权限都是正确的。我将Model.php、两个模型和控制器放在这里给你看:

Module.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();
}
}

型号 -> CategoriaGrupoTable.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();
    }
}

?>

型号 -> CategoriaGrupo.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);
    }
}

?>

好吧..这就是问题所在,瞧源文件...如果有人能给我点亮,我将不胜感激!我为我的英语感到抱歉。我正在尝试 2 周后发现错误。谢谢。

我建议您阅读 zend framewrok 2 的文档,以更好地了解查询的工作原理。

现在是解决方案。在

模型 -> CategoriaGrupoTable.php 修改 listar() 函数

 public function listar()
    {
        $result =  $this->tableGateway->select();
        return $result->toArray();
    }

感谢您的宝贵时间和帮助。我发现了问题:在 exchangeArray 方法中我减速了两次 "titulo"。这导致我的 table 的整个数据都为 null 或空,除了 slug...谢谢。