3

Overview: I have a single Joomla website that have various copies of the database tables all in the same database, but with different table prefixes.

Depending on which user is accessing the site, i need to alter which joomla table prefix is used in order to display the relevant tables.

This has to be site wide.

Problem: with a core code hack i can alter the prefix as needed, but would prefer this as a plugin.

With the plugin i can get the prefix, using $db->getPrefix(), and i can set an immediate instance of the connection using $db = JDatabaseDriver::getInstance( $option ). But this is not site wide and is overwritten by the standard $db = JFactory::getDbo().

So i need a system plugin that will replace every instance of a table prefix as it is run.

Any ideas?

  • Hi Shaun and welcome to JSE. I'm very curious as to why you'd need to change the table prefix per user. Could you shed some light on this? – Lodder Oct 26 '17 at 14:29
1

There is absolutely no way to do this from within a plugin, this is because Joomla starts accessing the database way before loading the plugins.

The only way to do this is from within the configuration.php file, where you should have the logic (you should create a function called getTablePrefix there). You must ensure though that Joomla cannot write to this file, because if it does so, then it will destroy your changes.

I have thought about doing this through the defines.php file, but it can't be done there because it is loaded before the configuration is loaded.

Excellent question by the way, but I'm also curious on why you need to have a different set of tables for each user. Maybe the best solution would be to rethink the whole logic.

0

I guess the only way to do it is to add your custom class JDatabaseDriverCustom extends JDatabaseDriverMySql for example and there you'll be able to go with a few options:

  • write user-depended logic right inside of the driver;
  • call some function that you will write in another place;

Anyway, think about: do you really need the site to depend on the user, or on something like IP or anything not related to Joomla. This way you'll be able to write __constructor() inside your config file and place the logic right there and change $this->db_prefix.

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.