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.

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

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题