No Code Attached Yet bug
avatar Flowman
Flowman
28 Jun 2023

Steps to reproduce the issue

I am trying to use this code to load the User table.

$table = Factory::getApplication()->bootComponent('com_users')->getMVCFactory()->createTable('User', 'Joomla\\CMS\\Table\\');

But the createTable strips all \\ and the prefix becomes JoomlaCMSTable and that wont work.

Old school code like this works but it is deprecated and will be removed.

$table = Table::getInstance('User', 'Joomla\\CMS\\Table\\')

We should be able to do the same through the createTable function.

Expected result

The table should be loaded.

Actual result

Null is returned as the class name is stripped.

System information (as much as possible)

PHP Version | 8.0.28
nginx/1.23.1
Joomla! 4.3.2 Stable [ Bora ] 30-May-2023 16:00 GMT

Additional comments

avatar Flowman Flowman - open - 28 Jun 2023
avatar joomla-cms-bot joomla-cms-bot - change - 28 Jun 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 28 Jun 2023
avatar richard67
richard67 - comment - 1 Jul 2023

@Flowman I think you misunderstand the meaning of the 2nd parameter $prefix of the createTable function. The prefix should be according to the application name, e.g. "Administrator" or "Site".

You can see that here in the createTable function's code https://github.com/joomla/joomla-cms/blob/4.3-dev/libraries/src/MVC/Factory/MVCFactory.php#L215-L234 and here in the getClassName method https://github.com/joomla/joomla-cms/blob/4.3-dev/libraries/src/MVC/Factory/MVCFactory.php#L260-L273 .

In your example this means you should use:

$table = Factory::getApplication()->bootComponent('com_users')->getMVCFactory()->createTable('User', 'Administrator');

To avoid deprecated logs (if switched on) and be prepared for the future, you should also pass the database object with the optional 3rd parameter $config:

$table = Factory::getApplication()->bootComponent('com_users')->getMVCFactory()->createTable('User', 'Administrator', ['dbo' => $db]);

with $db being the database object. In a CMS plugin you can just use $this->db.

Let me know if this works for you and we can close this issue.

Thanks in advance.

avatar richard67 richard67 - change - 1 Jul 2023
Labels Added: Information Required
avatar richard67 richard67 - labeled - 1 Jul 2023
avatar Flowman
Flowman - comment - 3 Jul 2023

This return NULL

$table = Factory::getApplication()->bootComponent('com_users')->getMVCFactory()->createTable('User', 'Administrator');

It is because the user table is in the Library\src\Table location and not in the com_user component.

That is why if you look anywhere in the code that requires the User table it still uses the legacy function.

$table = Table::getInstance('User', 'Joomla\\CMS\\Table\\')

Will the User table be moved in Joomla 5.0?

avatar Flowman Flowman - change - 3 Jul 2023
The description was changed
avatar Flowman Flowman - edited - 3 Jul 2023
avatar richard67 richard67 - change - 11 Jul 2023
Labels Removed: Information Required
avatar richard67 richard67 - unlabeled - 11 Jul 2023
avatar Hackwar Hackwar - change - 24 Aug 2023
Labels Added: bug
avatar Hackwar Hackwar - labeled - 24 Aug 2023
avatar laoneo
laoneo - comment - 24 Aug 2023

Use the old way with getInstance as the user table is still in the main namespace in libraries. For now there is no other way and it will not be moved in 5.

avatar joomdonation joomdonation - close - 24 Feb 2024
avatar joomdonation
joomdonation - comment - 24 Feb 2024

As Table::getInstance was deprecated, I would use the following code :

  • If the table belong to a component, use the recommended syntax:
Factory::getApplication()->bootComponent('...')->getMVCFactory()->createTable($name, $prefix, $config)
  • Otherwise, use new directly
$table = new \Joomla\CMS\Table\User($db);

You could get $db from container like:

$db = Factory::getContainer()->get(DatabaseInterface::class);

Or if inside a model

$db = $this->getDatabase();

I think nothing more we can discuss here, so I'm closing this issue. Feel free to open if you think there is still something to discuss.

avatar joomdonation joomdonation - change - 24 Feb 2024
Status New Closed
Closed_Date 0000-00-00 00:00:00 2024-02-24 11:15:45
Closed_By joomdonation

Add a Comment

Login with GitHub to post a comment