简体   繁体   中英

Zend Framework 2 returning object as a query result

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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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