在joomla中读取db的错误

时间:2010-09-13 15:28:30

标签: joomla

当我运行一个组件时,我得到 500 - 在joomla中读取db 时发生错误。

我的配置文件很完美。

我不知道还有什么可以改变......

任何指导都会有所帮助

提前致谢...

//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(); 
}

} ?&GT;

1 个答案:

答案 0 :(得分: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属性,您不需要将db变为变量。您可以像$this->_db一样访问它。您可以阅读Joomla Model class here

同样在您的模型中,您正在使用

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

模型有加载对象列表的简化方法,如此

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