1

In my custom Joomla component, I got the below code

$db = Joomla\CMS\Factory::getDbo();
//some code
if (!$row->bind($this->_formdata)) {
  if($db->getErrorMsg()){
    //do some code
  }
  return false;
}
//some code

I understand that the method getErrorMsg is deprecated in Joomla 4. What is the best way to get the DB error inside the model store method.

0
3

I try to give just a short answer on this. We cannot say that there is an “equivalent” of your code snippet in Joomla 4 since Joomla has been handling errors with using core PHP Exceptions. And your code sample would get error in newer Joomla 3 also.

The old JError class were deprecated quite long time ago (at Joomla 3.1). The code editors shows this obviously at older error handlings in codes inside Joomla (at getError(), setError(), getErrorMsg() and so on…)

The bind() method - if it is the Table class method - is throwing an Exception \InvalidArgumentException, if the data (first parameter of the method: $src) is not an object or an array. Or if that method would be the Joomla Form class bind() method then it thrown an \UnexpectedValueException.

The important part to understand at this question is that we have to catch the \Exception thrown in the method (otherwise it will cause a PHP Fatal error of Uncaught exception…), so we have to use a try-catch block in this code and catch the Exception given in the method bind() which we are using in this part of the code.

try 
{
    $row->bind($this->_formdata);
} 
catch (\InvalidArgumentException $e)
{
    // $e->getMessage(); // Exception message already set in the method we try to use...
    // do some code using the $e->getMessage(), but do not return it...
    // Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); // for example
    // or log it: Log::add($e->getMessage(), Log::WARNING, 'jerror');
    // or you can ignore the Exception
    return false;
}

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.