8

I have been looking through some logs and noticed that getError() and getErrors() are throwing deprecated messages in Joomla 3.3 (we just updated). Apparently these were deprecated back in Joomla 2.5, but I am having a hard time finding a solution to update to.

I took a look through the Joomla core files and even they are still using getError() and getErrors(). Is there a replacement for these, or is it like JViewLegacy, etc. where there isn't an alternative at the moment?

An example where I'm using it is for saving a user:

if (!$user->save()) {
        $error = $user->getError();
        ...
}
3

You should use PHP Exceptions instead. I do find it odd that Joomla throws deprecated messages when using JError yet isn't still used in the 3.3.6 core.

Anyway, the below would be an example of Exceptions for a database query

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('username')))
      ->from($db->quoteName('#__user'))
      ->where($db->quoteName('username') . ' = ' . $db->quote('Lodder'));
$db->setQuery($query);

try
{
    // Try to get an object based on the results from the database
    $results = $db->loadObjectList();
    print_r($results);
}
catch (Exception $e)
{
    // Start crying cause it didn't work
    echo $e->getMessage();
}

This will throw an error saying the table does not exist because I have used #__user instead of #__users.

In your case, I would try the following:

try
{
    $user->save();
    // redirect or do something magical
}
catch (Exception $e)
{
    echo $e->getMessage();
}

$e->getMessage() of course gets the error, so inside the catch, you can choose what you wish to do with it, such as log it or simply display it.

  • Will $e->getMessage() return the same error messages as the getError() call (localized as well)? – DemonGyro Oct 20 '14 at 20:27
  • I found time to implement the changes you suggested and it didn't work. save() isn't throwing the errors I'm expecting as exceptions, so the catch code is never run. – DemonGyro Oct 21 '14 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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