Multiple use of Joomla API instances in plugin helper file

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
sunit
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Tue Jan 10, 2006 7:26 am
Location: India

Multiple use of Joomla API instances in plugin helper file

Post by sunit » Sat Apr 04, 2020 6:18 pm

Hi,

I am working on a Joomla based application and earlier I was using innumerable database queries in modules and some of them are the same and repeatable due to availability on different pages.

To get rid off the same repeatable queries and reduce the database load, I have built a helper file in system plugin and put all custom queries in separate function within a single class. The all queries count is around 40.

But in each function, I have to call the below code to run the query:

Code: Select all

$user = JFactory::getUser();
	$userid = $user->get('id');
	$db = JFactory::getDbo();
I think that Joomla has to run the above Joomla API instances around 40 times for each functions to run the queries.

Now I want to ask, Is this practice is ok as the Joomla API framework is already loaded and I can call the above APIs innumerable times in each function?

OR

How can I use the single instances of these APIs in helper class, if it is possible how to use it and where to use it?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17426
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Multiple use of Joomla API instances in plugin helper file

Post by toivo » Sun Apr 05, 2020 8:46 am

The Factory class follows the singleton design pattern and takes care of the single instances. There is no need to duplicate them in your code. Here is Factory::getDbo() as an example from 3.9.16:

Code: Select all

	/**
	 * Get a database object.
	 *
	 * Returns the global {@link \JDatabaseDriver} object, only creating it if it doesn't already exist.
	 *
	 * @return  \JDatabaseDriver
	 *
	 * @see     \JDatabaseDriver
	 * @since   1.7.0
	 */
	public static function getDbo()
	{
		if (!self::$database)
		{
			self::$database = self::createDbo();
		}

		return self::$database;
	}
Toivo Talikka, Global Moderator

sunit
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Tue Jan 10, 2006 7:26 am
Location: India

Re: Multiple use of Joomla API instances in plugin helper file

Post by sunit » Sun Apr 05, 2020 1:24 pm

When I removed the $db = JFactory::getDbo(); from function, it doen not work. However, the single instance works on the code page if it is declared on the top and the DB query is not wrapped in a function. The same case when we declare this DB instance in a function within the class than it is available to all the queries within and only to that function. But as usual, all functions contain the separate queries and in each case I have to instantiate that DB instance to run the query within the function.

I have just gone through the document here:https://docs.joomla.org/Selecting_data_using_JDatabase

It says:
Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats.
This means, as I understand that, that we have to instantiate that DB instance to run the query within the function.

However, I have not access to any third party developer's helper class, how they use the DB instance.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17426
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Multiple use of Joomla API instances in plugin helper file

Post by toivo » Sun Apr 05, 2020 2:32 pm

sunit wrote:It says:
Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats.
This means, as I understand that, that we have to instantiate that DB instance to run the query within the function.
No, we do not have to because in the Factory class getDbo() calling createDbo() does it for us:
...the class automatically creates the database connection.
Have a look at administrator/components/com_banners/helpers.php. On the other hand, it is not necessary to have a helper file but to call JFactory::getDbo() in the model to get the database connection object when required, for example in components/com_banners/models/banners.php.
Toivo Talikka, Global Moderator


Locked

Return to “Joomla! 3.x Coding”