douruyun8153 2016-05-31 08:43
浏览 34

Zend Framework 2将对象作为查询结果返回

I have a query in ZF2 like following:

public function BrandWallBrandsById($userId,$brandId,$startDate,$endDate)
    {
            $select = new Select();
            $select->from(array('p' => $this->table), array('id'));
            $select->join(array('b' => 'brands'), 'p.brandId = b.id', array('id','name', 'cover', 'slogan', 'startDate', 'homepage'));
            $select->columns(array(new Expression('SUM(p.points) as points'), "userId", "brandId"));
            $select->order("p.points desc");
            $select->group("p.brandId");
            $where = new Where();
            $where->notEqualTo("p.points", 0);
            $where->equalTo("p.userId", $userId);
            $where->equalTo("p.brandId", $brandId);
            $where->between("p.time", $startDate, $endDate);
            $select->where($where);
            return $this->historyTable->selectWith($select)->toArray();

    }

Since I'm returning only a single object, can I return an object from this query instead of an array[0] ?? Is there any way to do this ?

  • 写回答

1条回答 默认 最新

  • doushi4633 2016-05-31 09:48
    关注

    If you're expecting a signle row from your query then you can use current(). Modify the last line of your code, juste like this:

    $row = $this->historyTable->selectWith($select)->current();
    if(!$row){
        throw new Exception('No row found');
    }
    return $row;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题