简体   繁体   English

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

[英]Zend Framework 2 returning object as a query result

I have a query in ZF2 like following: 我在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();

    }

Since I'm returning only a single object, can I return an object from this query instead of an array[0] ?? 由于我只返回一个对象,我可以从此查询而不是数组[0]返回一个对象吗? Is there any way to do this ? 有没有办法做到这一点?

If you're expecting a signle row from your query then you can use current() . 如果您期望查询中出现一个单行,那么您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM