0
votes

Going from Joomla 2.5 to 3.0 with my extension, I'm struggling with how to do the DB error handling (since GetErrorNum is deprecated, see also Joomla! JDatabase::getErrorNum() is deprecated, use exception handling instead).

The way that seems to be the one to go according to the question linked above, is to add the following code for each db->query() code:

if (!$db->query()) {
    throw new Exception($db->getErrorMsg());
}

In my opinion, that makes DB error handling more awkward than it was before. So far, I simply called a checkDBError() function after a DB call, which queried the ErrorNum and handled any possible error accordingly.

That was independent from how the DB query was actually triggered - there are different ways to do that, and different results on an error: $db->loadResult() returns null on error, $db->query() returns false. So there will now be different checks for different DB access types.

Isn't there any generic way to handle this, e.g. a way to tell Joomla to throw some exception on DB problems? Or do I have to write my own wrapper around the DatabaseDriver to achieve that? Or am I maybe missing something obvious?

Or should I just ignore the deprecation warning for now and continue with using getErrorNum()? I'd like to make my extension future-proof, but I also don't want to clutter it too much with awkward error handling logic.

2
@user3145373 how on earth would that other question be a duplicate? this one is about generic database error handling in Joomla, esp. 3.0, while the one you linked is about uploading a file in Joomla 2.5 - and while there is some DB access there, I don't see any error handling going on therecodeling

2 Answers

2
votes

Just found this discussion: https://groups.google.com/forum/#!msg/joomla-dev-general/O-Hp0L6UGcM/XuWLqu2vhzcJ

As I interpret it, there is that deprecation warning, but there is no proper replacement yet anyway...

Unless somebody points out any other proper documentation of how to do it in 3.0, I will keep to the getErrorNum method of doing stuff...

-1
votes

Get getErrorNum() function will solve your problem....

   $result = $db->loadResult();

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

        return false;
    }