php - Zend 2 Ajax JSON 多数据

标签 php jquery ajax json zend-framework2

我试图弄清楚如何使用 JSON 通过 Ajax 显示一些元素。我关注了这个Using ajax in zend framework 2和everting工作正常,但现在想通过JSON传递例如10个对象

public function indexAction()
{   
    $result = new JsonModel (array(
        'fototable' => $this->FotoTable->getFotos()
        )
    );
    return $result;
}

然后循环js并显示数据。问题是如果我执行上面的代码。我没有得到任何 Json 输出。我尝试序列化数据,但收到 PDO 语句无法序列化的错误。

这是我的 Fototable 类:

<?php

namespace Media\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Adapter\Adapter;

class FotoTable extends TableGateway{

    public static $tableName = 'media';

    public function addFoto(array $data)
    {
        return $this->insert($data);
    }

    public function updateFoto(array $insertData, $id)
    {
        return $this->update($insertData, array('id' => $id));
    }

    public function getFotos()
    {
        $select = new Select();
        $select->from(self::$tableName);
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();

        return $resultSet;      
    }

    public function getFotoById($id)
    {
        $select = new Select();
        $select->from(self::$tableName);
        $select->where(array('id' => $id));
        return $this->selectWith($select)->current();
    }

    public function getFotosByObject($project)
    {
        $select = new Select();
        $select->from(self::$tableName);
        $select->where(array('object_id' => $project));

        return $this->selectWith($select);
    }
}

最佳答案

public function getFotos()
{
    $select = new Select();
    $select->from(self::$tableName);
    $resultSet = $this->selectWith($select);
    $resultSet->buffer();

    return $resultSet;      
}

您返回的结果集不是数组。你可以这样做

return $resultSet->toArray();

有时 to array 不可用,因此您可以将 resultSet 迭代到数组

$array = array();
foreach ($resultSet as $row) {
    $array[] = $row
}
return $array;

关于php - Zend 2 Ajax JSON 多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27076251/

相关文章:

php - 为我的 MySQL 选择分配 Row_Number

javascript - 创建动态跨度

javascript - 删除Jquery字符串中的最后8个字符

javascript - jquery 函数被调用两次

php - MySQL上5个表的字段总和

php - 如何将项目添加到可以在另一个页面上访问的javascript数组?

php - 带有动态属性名称的 isset()

jQuery 在悬停时显示内容

javascript - 单击时显示下一个 div

javascript - Rails 4 - 渲染部分后运行代码的事件