2016-05-31 33 views
1

我有一个像以下在ZF2的查询:Zend框架2返回对象作为查询结果

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] ??有没有办法做到这一点?

回答

0

如果您希望查询中有一个标牌行,那么您可以使用current()。修改代码的最后一行,如下所示:

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