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

时间:2016-05-31 08:43:24

标签: php mysql zend-framework zend-framework2

我在ZF2中有一个查询,如下所示:

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();

    }

由于我只返回一个对象,我可以从此查询而不是数组[0]返回一个对象吗?有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您希望从查询中获得单行,则可以使用current()。修改代码的最后一行,如下所示:

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