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.
The table should be loaded.
Null is returned as the class name is stripped.
PHP Version | 8.0.28
nginx/1.23.1
Joomla! 4.3.2 Stable [ Bora ] 30-May-2023 16:00 GMT
Labels |
Added:
No Code Attached Yet
|
Labels |
Added:
Information Required
|
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?
Labels |
Removed:
Information Required
|
Labels |
Added:
bug
|
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.
As Table::getInstance
was deprecated, I would use the following code :
Factory::getApplication()->bootComponent('...')->getMVCFactory()->createTable($name, $prefix, $config)
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.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-02-24 11:15:45 |
Closed_By | ⇒ | joomdonation |
@Flowman I think you misunderstand the meaning of the 2nd parameter
$prefix
of thecreateTable
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 thegetClassName
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.