Ready-made extensions and custom development for Joomla!

  • Home
  • Blog
  • Joomla dev best practices: latest news from JDatabase

Joomla dev best practices: latest news from JDatabase

It's been almost one year since my series about Joomla Extensions Development Best Practices started.

As you may guess, during this period I improved my knowledge and I'm ready to propose additional and/or reviewed best practices.

I will start today with a few more words about using JDatabase and its related classes.

JDatabaseQuery

In our original MVC sample code, we used text queries built with sprintf. 

It turned out that we were missing a much better way to write queries, by using the JDatabaseQuery class.

Here's how it works:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('name')
->from('`#__users`')
->where('`id` = 1')

This code is intentionally very simple, missing quote and quoteName methods, to show you the basic.

The nicest thing is that now $query is an object and you can easily modify it later on, without caring about the order: the real query string will be built only at the end.

For example, you can add a select like this some lines below:

$query->select('email');

and it's ok.

Without database query, you have more constraints while building the query.

You can handle almost all queries with this class, and once you start it you will realize you cannot ever do without it.

For more info, take a look at the documentation on Joomla API site.

Obsolete methods

There are quite a few deprecated methods in the 2.5 API, which are removed in 3.2 or will be removed in a near future.

For the full list you can check the JDatabaseMysql doc page on Joomla API site, but here we will see a few important cases.

  • nameQuote: use quoteName instead (yeah, I know...)
  • loadResultArray: use loadColumn instead (this is one I used a lot)
  • getErrorMsg and getErrorNum: use try / catch block instead (more about that in a minute)

As I said the list contains many more, but those are the ones who make the most difference.

Try and catch

Instead of reading an error number or just run a boolean check to see if a query has run correctly, you should always use a try / catch block.

That is possible because the query execution will always raise an exception if something goes wrong.

So, assuming your $query object is already prepared as we've seen above, your code will look similar to this:

$db->setQuery($query);
try {
$user = $db->loadAssoc();
return $user;
}
catch (exception $e) {
return array();
}

Again, this code is very simple: we basically ignore whatever exception is raised and just return an empty array so the flow will not break.

This is something we do a lot.

But you may as well decide to, e.g.

  • set an application message to show to the user
  • log your error somewhere

The use of JDatabaseQuery and try / catch block may make your code a bit large, but that's an excellent opportunity to take the code out in a separated short method.

So all in all I like this new way of doing things.