5

I was trying to develop Joomla 2.5 and 3 component. As many things deprecated between these two versions and the way to throw and catch database and other errors, I could not find ways yet to make single component for both 2.5 and 3.

So is making component separately for 2.5 and 3.

Firstly, in 2.5 this way I am catching an error in database: -

if(!$database->query())
{ 
    return false; 
}

But how do I show or return exact error message to the view class so that error is displayed in the component?

Secondly, for another kind of db query, I am getting data from db this way in model class (here is my code in model's function):

$this->_data = null; 
if(JFactory::getApplication()->input->get('id') > 0) {
    $db = JFactory::getDBO();
    $db->setQuery("select * from `#__tablename_wrong` where `id`='".JFactory::getApplication()->input->get('id')."'");
}
if ($db->getErrorNum()) {
    JError::raiseWarning(500, $db->getErrorMsg());
}
$this->_data = $db->loadObject(); 
return $this->_data; 

Now I can inspect $this->_data in view class to see if 0 records return but again how do I display a specific database error message or return an error message to my view class to show errors?

So these are the most common ways developers should show errors while doing database queries in 2.5.

1
  • You asked a similar question before and haven't yet provided the full code - it might be better to do that first. Apr 30, 2014 at 17:02

2 Answers 2

6

Pretty much the same as @ValentinDespa 's answer but i'm just gonna merge the two things together into something that will work in 2.5 and 3.x.

This rather than displaying a full page error message just returns false and then inserts an error message into the top of the page. Hope that's what you're after

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select('X') // This is specially added to throw an SQL error.
      ->from('#__users');
$db->setQuery($query);

if(!JError::$legacy)
{
    try
    {
        // If it fails, it will throw a RuntimeException
        $result = $db->loadResult(); 
    }
    catch (RuntimeException $e)
    {
        JFactory::getApplication()->enqueueMessage($e->getMessage());

        return false;
    }
}
else
{
    $result = $db->loadResult();

    // Check for a database error.
    if ($db->getErrorNum())
    {
        JFactory::getApplication()->enqueueMessage($db->getErrorMsg());

        return false;
    }
}

The JError legacy switch was false in 2.5 to use JError and then true in 3.x to use exceptions.

You can see a full example of me using this in one of my modules here https://github.com/JoomJunk/shoutbox/blob/development/mod_shoutbox/helper.php#L33

2
  • Considering that to create single component for both 2.5 and 3, many things in joomla version < 2.5.5 would not work.So it's better to have separate component for 2.5 and 3 with identifying error efficiently, no need to go through complex code.Hope this post helps everybody well.
    – dev-m
    May 2, 2014 at 15:17
  • Pretty much the only difference you need to consider with <2.5.5 is that we introduced the Legacy MVC classes. The majority of extensions only support the latest Joomla versions of 2.5 and 3.x anyhow so I wouldn't say that matters too much either. However there are workarounds even for that (e.g github.com/joomlaworks/k2/blob/master/administrator/components/… which is yucky code but works ......) May 2, 2014 at 16:58
4

Watch out for exposing too much info! Please note that by throwing SQL errors into the wild, you may expose your database structure or other sensitive information. I would throw SQL errors only in backend (to let's say trusted users) and in frontend just a generic error and I would log the exact error in a error log.

Doing a query in 3.x catching and throwing exceptions

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select('X') // This is specially added to throw an SQL error.
      ->from('#__users');

try
{
    $db->setQuery($query);
    $result = $db->loadResult(); // If it fails, it will throw a RuntimeException 
}
catch (RuntimeException $e)
{
    throw new Exception($e->getMessage());
}

Doing a query in Joomla 2.5 and 3.x using old deprecated functions

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select('X'); // This is specially added to throw an SQL error.
      ->from('#__users');
$db->setQuery($query);

// Check for a database error.
if ($db->getErrorNum()) {
    JError::raiseWarning(500, $db->getErrorMsg());
}

$result = $db->loadResult();
2
  • here is my code in model's function - $this->_data = null; if(JFactory::getApplication()->input->get('id') > 0) {$db = JFactory::getDBO();$db->setQuery("select * from ``#__tablename_wrong`` where ``id``='".JFactory::getApplication()->input->get('id')."'"); } if ($db->getErrorNum()) { JError::raiseWarning(500, $db->getErrorMsg()); } $this->_data = $db->loadObject(); return $this->_data; but in your this way i see no errors anywhere in 2.x. but your 3.x one shows a whole error page instead error at top of component.I like your idea,but what "generic error" ?
    – dev-m
    Apr 30, 2014 at 23:34
  • You shouldn't be using JError. Enqueue the message instead. Also would you please put this code in your question since it's really hard to read here.
    – Elin
    May 2, 2014 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.