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 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页