doutan5798 2014-10-04 15:48 采纳率: 100%
浏览 40
已采纳

ZF2:TableGateway ResultSet到JSON

I am trying to return data from my database to JSON in Zend Framework 2. I am using a TableGateway right now and it returns a ResultSet. But the JsonModel cannot show the resultset. So is there a way to convert it, or is there an other way to access my database?

The IndexAction in the HomeController

public function indexAction()
{
    return new JsonModel(array(
        'posts' => $posts,
        'resources' => $resources,
        'style' => $style,
        'imgStyle' => $imgStyle,
        'success' => true,
    ));
}

The function that returns the resultset

public function fetchAll()
{
    $resultSet = $this->tableGateway->select(function (Select $select) {
        $select->order('date DESC');
    });
    return $resultSet;
}
  • 写回答

1条回答 默认 最新

  • doutong2132 2014-10-06 08:24
    关注

    Considering you have already done this in module.config.php:

    'view_manager' => array(
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),
    

    My next suggestion would be to convert the result to array.

    $resultSet->toArray();
    

    OR

    $return = array();
    
    foreach( $resultSet as $r )
        $return[]=$r;
    

    Your return new JsonModel, is correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 mysql做碎片化处理老是报错怎么办
  • ¥15 如何正确在vs2010中初始化map对象
  • ¥30 mmdet3d模型部署问题
  • ¥15 comsol仿真反射率、吸收率时峰值位置和深度不对!
  • ¥30 Visual Studio找不到sdk,如何解决?
  • ¥15 labview与机器之间网口通信发送指令
  • ¥15 迪杰斯特拉算法,考虑时延,带宽
  • ¥30 C#提交json字符串给http服务器