2017-06-29 96 views
0

我嘗試了不同的可能性,但沒有任何工作,在教程中我找不到示例。語法用於選擇我的模型中的最後一個記錄集

我在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; 

但如何將在這種情況下正確的語法?

回答

1

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,這是很容易的 –

相關問題