在ZF2 AbstractTableGateway上具有多个结果集的条件

时间:2018-03-22 09:38:33

标签: php zend-framework2 zend-db

在Zf2应用程序中编写模型文件以从表中检索数据集, 它可以按预期的方式返回一个结果集,但是返回多行不能通过下面的代码实现。

  

工作和返回单行

/**
     * @param $id
     * @return bool|Entity\Feeds
     */
    public function getAppFeed($id)
    {
        $row = $this->select(array('app_id' => (int)$id))->current();
        if (!$row)
            return false;
        $feedVal = new Entity\Feeds(array(
            'id' => $row->id,
            'title' => $row->title,
            'link' => $row->link,
            'Description' => $row->description,
            'created' => $row->created,
        ));
        return $feedVal;

    }

删除当前并尝试过的tablegateway对象,但抛出错误。

Feeds表将为每个应用程序提供多条记录,我需要一个功能来实现相同的目的。

1 个答案:

答案 0 :(得分:1)

Select始终返回ResultSet。您可以通过遍历它来访问ResultSet的对象(1),因为它实现了Iterator接口。

只是一段代码示例:

public function getAppFeed($id)
{
    $resultSet = $this->select(array('app_id' => (int)$id));

    if ($resultSet instanceof \Zend\Db\ResultSet) {
        foreach($resultSet as $item) {
            // do your feed stuff here
            // e.g. $item->id
        }
    } else {
        return false;
    }
}

(1)对象:表示您在Prototype中将TableGateway作为对象的任何对象。

有关详细信息,请查看ResultSet的文档。

相关问题