2011-07-13 69 views
8

我創建了一個函數來獲取最後一個字段的值在表像這樣ZF:無效的參數號:無參數綁定錯誤

private function _getLastPosition ($menuId) { 
    $select = $this -> getDbTable() -> select(); 
    $select -> where("menu_id = ?", $menuId) 
      -> order('position DESC'); 
    $row = $this -> getDbTable() -> fetchRow($select); 
    if($row) { 
     return $row -> position; 
    } else { 
     return 0; 
    } 
} 

當我運行它,我得到

消息:SQLSTATE [HY093]:無效的參數號:無參數的約束

請幫我解決這個問題

回答

12

它通常意味着$ menuId爲空/ NULL。在$ select-> where()調用中使用$ menuId之前,請確保$ menuId變量在使用$ select之前具有適當的值:

您可以在函數的開頭添加以下行:

if(empty($menuId)) 
    return 0; 

這將情況返回0,沒有$菜單Id提供了依據。如果你想扔在這種情況下錯誤(異常),你可以做到以下幾點:

if(empty($menuId)) 
    throw new Exception("No Menu ID Provided"); 

你完整的功能應該是這樣的:

private function _getLastPosition ($menuId) { 
    if(empty($menuId)) 
     throw new Exception("No Menu ID Provided"); 
    $select = $this -> getDbTable() -> select(); 
    $select -> where("menu_id = ?", $menuId) 
      -> order('position DESC'); 
    $row = $this -> getDbTable() -> fetchRow($select); 
    if($row) { 
     return $row -> position; 
    } else { 
     return 0; 
    } 
} 
+0

在那裏把這段代碼。 PS我是新手 – mrN

+1

是的,它是空的。謝謝我修復它。但其他錯誤也彈出:(:) – mrN

+0

哪些錯誤是彈出? –