2010-09-13 110 views
0

雖然我運行一個組件我得到500 - 發生錯誤讀取數據庫中的Joomla。錯誤讀取數據庫

我的配置文件是完美的。

,我不知道還有什麼改變..

任何指導意見將有助於

在此先感謝...

//No direct acesss 
defined('_JEXEC') or die(); 

jimport('joomla.application.component.model'); 

class DealsModelDeals extends JModel { 

function getDeals(){ 
    $db = $this->getDBO(); 

    $db->setQuery('SELECT * from #__todaysdeal'); 
    $deals = $db->loadObjectList(); 

    if ($deals === null) 
    JError::raiseError(500, 'Error reading db'); 

    return $deals; 
} 

function getDeal($id){ 
    $query = ' SELECT * FROM #__todaysdeal '. ' WHERE id = '.$id; 
    $db = $this->getDBO(); 
    $db->setQuery($query); 
    $deal = $db->loadObject(); 

    if ($deal === null) 
    JError::raiseError(500, 'Deal with ID: '.$id.' not found.'); 
    else 
    return $deal; 
} 


/** 
     * Method that returns an empty greeting with id 0 
     * 
     * @access public   
*/ 

function getNewDeal(){ 
    $dealTableRow =& $this->getTable('deals'); 

    $dealTableRow->id=0; 
    $dealTableRow->clientName=''; 
    return $dealTableRow; 
} 

/** 
     * Method to store a greeting in the DB 
     * 
     * @access public   
*/ 

function saveDeal($deal) 
{ 

    //Parameter not necessary because our model is named DealsModelDeals (used to ilustrate that you can specify an alternative name to the JTable extending class) 
    $dealTableRow =& $this->getTable('deals'); 

    //print_r($dealTableRow); 
    //print_r($_FILES); exit; 
    // Bind the form fields to the todaysdeal table 
    if (!$dealTableRow->bind($deal)) { 
     JError::raiseError(500, 'Error binding data'); 
    } 

    // Make sure the deal record is valid 
    if (!$dealTableRow->check()) { 
     JError::raiseError(500, 'Invalid data'); 
    } 

    // Insert/update this record in the db 
    if (!$dealTableRow->store()) { 
     $errorMessage = $dealTableRow->getError(); 
     JError::raiseError(500, 'Error binding data: '.$errorMessage); 
    } 

    $id = $dealTableRow->id; 

    if(!empty($_FILES['dealImage'])) 
    { 

     $file = $_FILES['dealImage']; 
     $id = $dealTableRow->id; 

     if ((($_FILES["dealImage"]["type"] == "image/gif") || ($_FILES["dealImage"]["type"] == "image/jpeg") || ($_FILES["dealImage"]["type"] == "image/pjpeg")) && ($_FILES["dealImage"]["size"] < 150000)) 
     { 
      if ($_FILES["dealImage"]["error"] > 0) 
      { 
       echo "Return Code: " . $_FILES["dealImage"]["error"] . "<br />"; 
      } 
      else 
      { 
       if (file_exists("components/com_deals/dealImages/" . $_FILES["dealImage"]["name"])) { 
        $_FILES["dealImage"]["name"] . " already exists. "; 
       } else { 
        move_uploaded_file($_FILES["dealImage"]["tmp_name"], "components/com_deals/dealImages/" .$id."_".$_FILES["dealImage"]["name"]); 
        echo "Stored in: " . "dealImages/" . $_FILES["dealImage"]["name"]; 
       } 
      } 
     } 
     else 
     { 

     } 
    } 

    $dealImage = $_FILES['dealImage']['name']; 
    $dealImage .= (!empty($_FILES['dealImage']['name'])) ? ' ' . $_FILES['dealImage']['name'] : ''; 
    $query = "UPDATE #__todaysdeal SET dealImage='".$id."_".$_FILES['dealImage']['name']."' WHERE id='".$id."'"; 

    $db = $this->getDBO(); 
    $db->setQuery($query); 
    $db->query(); 
    //If we get here and with no raiseErrors, then everythign went well 
} 

function deleteDeals($arrayIDs) 
{ 
    $query = "DELETE FROM #__todaysdeal WHERE id IN (".implode(',', $arrayIDs).")"; 
    $db = $this->getDBO(); 
    $db->setQuery($query); 
    if (!$db->query()){ 
     $errorMessage = $this->getDBO()->getErrorMsg(); 
     JError::raiseError(500, 'Error deleting Deals: '.$errorMessage); 
    } 
} 

function dealsUploadPhoto($file, $id) 
{ 
    //UPLOAD FILE 

      $config = & JComponentHelper::getParams('com_deals'); 
      $allowed = array('image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/x-png', 'image/gif', 'image/ico', 'image/x-icon'); 
      $pwidth = $config->get('pwidth'); 
      $pheight = $config->get('pheight'); 
      $maxsize = $config->get('maxsize'); 

      if($file['size'] > 0 && ($file['size']/1024 < $maxsize)){   

      if(!file_exists(JPATH_SITE . DS. 'images' . DS . 'deals')) 
      { 
       if(mkdir(JPATH_SITE . DS . 'images' . DS . 'deals')) { 
        JPath::setPermissions(JPATH_SITE . DS . 'images' . DS . 'deals', '0777'); 
        if(file_exists(JPATH_SITE . DS . 'images' . DS . 'index.html')) { 

         copy(JPATH_SITE . DS . 'images' . DS . 'index.html', JPATH_SITE . DS . 'images' . DS . 'deals/index.html'); 
        } 
        } 
      } 

       if($file['error'] != 0){ 
        tpJobsMsgAlert('Upload file photo error.'); 
        exit(); 
       } 

       if($file['size'] == 0){ 
        $file = null; 
       } 

       if(!in_array($file['type'], $allowed)) { 
        $file = null; 
       } 

       if ($file != null){ 
        $dest = JPATH_SITE.DS.'images'.DS.'deals'.DS.$id.'.jpg'; 

        if(file_exists($dest)) 
        { 
         $del = unlink($dest); 
        } 

        $soure = $file['tmp_name']; 
        jimport('joomla.filesystem.file'); 
        $uploaded = JFile::upload($soure,$dest); 

        $fileAtr = getimagesize($dest); 
        $widthOri = $fileAtr[0]; 
        $heightOri = $fileAtr[1]; 
        $type = $fileAtr['mime']; 
        $img = false; 
        switch ($type) 
        { 
        case 'image/jpeg': 
        case 'image/jpg': 
        case 'image/pjpeg': 
         $img = imagecreatefromjpeg($dest);      
         break; 
        case 'image/ico': 
         $img = imagecreatefromico($dest); 
         break; 
        case 'image/x-png': 
        case 'image/png': 
         $img = imagecreatefrompng($dest); 
         break; 
        case 'image/gif': 
         $img = imagecreatefromgif($dest); 
         break; 
        } 

        if(!$img) 
        { 
        return false; 
        } 

        $curr = @getimagesize($dest); 

        $perc_w = $pwidth/$widthOri; 
        $perc_h = $pheight/$heightOri; 
        if(($widthOri<$pwidth) && ($heightOri<$height)) 
        { 
        return; 
        } 

        if($perc_h > $perc_w) 
        { 
        $pwidth = $pwidth; 
        $pheight = round($heightOri * $perc_w); 
        } 
        else 
        { 
        $pheight = $pheight; 
        $pwidth = round($widthOri * $perc_h); 
        } 


        $nwimg = imagecreatetruecolor($pwidth, $pheight); 
        imagecopyresampled($nwimg, $img, 0, 0, 0, 0, $pwidth, $pheight, $widthOri, $heightOri); 

        imagejpeg($nwimg, $dest, 100); 
        imagedestroy($nwimg); 
        imagedestroy($img); 
       } 

      }else{ 
       if($file['size']/1024 > $maxsize){ 
        dealsMsgAlert('Size of file photo is too big. Maximum size".$maxsize." KB'); 
        exit(); 
       } 

      } 
} 

function dealsMsgAlert($msg) 
{ 
    if (!headers_sent()) 
    { 
     while(@ob_end_clean()); 
     ob_start(); 
     echo "<script> alert('".$msg."'); window.history.go(-1); </script>\n"; 
     $out = ob_get_contents(); 
     ob_end_clean(); 
     echo $out; 
     exit(); 
    } 
    echo "<script> alert('".$msg."'); window.history.go(-1); </script>\n";  
    exit(); 
} 

} ?>

+0

其成分是什麼? – silvo 2010-09-13 15:31:03

+0

我自己的組件..只是爲了保存數據到數據庫。我是組件新手。 – Fero 2010-09-13 15:32:31

+0

該網站本身的工作?只有你的組件有問題嗎?你連接到非本地數據庫? – Alex 2010-09-13 15:53:37

回答

0

的導致紅色屏幕出現500錯誤的問題正在發生,因爲如果請求的報價不存在,則引發異常。你不應該使用JError::raiseError()

改用下列​​之一:

// This will set error to the model. You can get the errors from 
// the model by your controller $model->getErrors() and output them to the screen. 
$this->setError('ERROR MESSAGE GOES HERE'); 

OR

// This will output errors to the screen right the way 
JFactory::getApplication()->enqueueMessage('ERROR MESSAGE GOES HERE', 'message'); 

型號已經_db財產,你不需要讓數據庫進入變量。您可以像這樣訪問它$this->_db。你可以閱讀約Joomla Model class here

而且在模型中使用的是

$db = $this->getDBO(); 
$db->setQuery('SELECT * from #__todaysdeal'); 
$deals = $db->loadObjectList(); 

模型簡化方法加載對象的名單,像這樣

$deals =& $this->_getList('SELECT * from #__todaysdeal'); 
+0

問題很可能不是錯誤得到提高,而是什麼導致錯誤被提出... – Martin 2010-09-14 12:56:40

+0

好,這是真的。如果他沒有得到'500 Error',他可以看到執行了哪些查詢,這只是幫助解決問題的一個步驟。我的猜測是,他的表沒有相應的名稱,沒有前綴,或者他只是沒有'#__ todaysdeal'和'getDeals()'中的數據導致錯誤,因爲這一點。 – Alex 2010-09-14 13:08:16