Zend framework 选择模型中最后一个记录集的语法

Zend framework 选择模型中最后一个记录集的语法,zend-framework,zend-db,zend-framework3,Zend Framework,Zend Db,Zend Framework3,我尝试了不同的可能性,但没有任何效果,在教程中我也找不到一个例子 我的模型类中有一个方法: public function getlastImport($filename) { //$id = (int) $id; $rowset = $this->tableGateway->select(['Path' => $filename]); $row = $rowset->current(); if (! $row) {

我尝试了不同的可能性,但没有任何效果,在教程中我也找不到一个例子

我的模型类中有一个方法:

    public function getlastImport($filename)
{
    //$id = (int) $id;
    $rowset = $this->tableGateway->select(['Path' => $filename]);
    $row = $rowset->current();
    if (! $row) {
        throw new RuntimeException(sprintf(
                'Could not find row with identifier %d',
                $id
                ));
    }

    return $row;
}
我想检索给定文件名的最后一次导入,因此ist必须与sql中的类似:

select max(ID) from table where filename = $filename;

但是在这种情况下,正确的语法是什么呢?

sql查询应该是

"SELECT * FROM table_name WHERE filename={$filename} ORDER BY id DESC LIMIT 1" 
在您的模型中使用以下选项:

public function getlastImport($filename)
{

    $select = $this->tableGateway->getSql()->select();
    $select->columns(array('id', 'filename', 'label'));
    $select->where(array('filename' => $filename));
    $select->order("id DESC");
    $select->limit(1);

    $result = $this->tableGateway->selectWith($select);
    $row = $result->current();

    if (! $row) {
        throw new RuntimeException(sprintf(
            'Could not find row with identifier %d',
            $id
        ));
    }

    return $row;
}

希望这对你有帮助

问题是,如何在我的模型中编写它。它工作了,多亏了fpr语法,我正在寻找实现rowfunction max的方法,这很容易