语法用于选择我的模型中的最后一个记录集

问题描述:

我尝试了不同的可能性,但没有任何工作,在教程中我找不到示例。语法用于选择我的模型中的最后一个记录集

我在modelclass的方法:

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; 
} 

希望这会帮助你!

+0

问题是,如何在我的模型中编写它。 –

+0

只需编辑代码!让我们知道它是否适合你! – unclexo

+0

它的工作,谢谢fpr的语法,我正在寻找执行rowfunction max,这是很容易的 –