tags:

views:

54

answers:

1

Hi all,

While i run a component i am getting 500 - An error has occurred error reading db in joomla.

My configuration file is perfect.

I don't know what else to change..

Any guidance will be helpful

Thanks in advance...

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

} ?>

A: 

The problem that causes the red screen with 500 Error is happening because you are raising an exception if the requested quote is does not exist. You should not use JError::raiseError().

Use one of the following instead:

// 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');

Model already has _db property, you do not need to get db into variable. You can access it like this $this->_db. You can read about Joomla Model class here.

Also within your model you are using

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

Model has simplified method to load list of object, like so

$deals =& $this->_getList('SELECT * from #__todaysdeal');
Alex
The problem is most likely not that the error is getting raised, but rather what is causing the error to be raised...
Martin
well that is true. He can see what queries are executed if he does not get `500 Error`, this is just a step to help resolve the problem. My guess is that his tables dont have corresponding names, left out prefix or he just does not have data in `#__todaysdeal` and `getDeals()` causes ERROR because of that.
Alex