我在 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]吗? 有什么办法可以做到这一点吗?
如果您期望查询中有符号行,则可以使用 current()
.修改代码的最后一行,就像这样:
$row = $this->historyTable->selectWith($select)->current();
if(!$row){
throw new Exception('No row found');
}
return $row;