When you try to get an instance of a table with JTable prefix, that has already been initialized, it throws an error.
Stack trace
0 Class "\Joomla\CMS\Table\Company" not found
Call Stack
1 () JROOT\libraries\src\Table\Table.php:344
Call JTable::getInstance($type, $prefix) twice for a table that has the JTable prefix (it works for tables with Table prefix)
Retrieve the table object
Throws an error
Joomla 5.0
If $tableClassLegacy has already been initialized, the $tableClass is not switched anymore to $tableClassLegacy and it throws an error.
// Only try to load the class if it doesn't already exist.
if (!class_exists($tableClass) && !class_exists($tableClassLegacy)) {
// Search for the class file in the JTable include paths.
$paths = self::addIncludePath();
$pathIndex = 0;
while (!class_exists($tableClass) && !class_exists($tableClassLegacy) && $pathIndex < \count($paths)) {
if ($tryThis = Path::find($paths[$pathIndex++], strtolower($type) . '.php')) {
// Import the class file.
include_once $tryThis;
}
}
if (!class_exists($tableClass) && class_exists($tableClassLegacy)) {
$tableClass = $tableClassLegacy;
}
if (!class_exists($tableClass)) {
/*
* If unable to find the class file in the Table include paths. Return false.
* The warning JLIB_DATABASE_ERROR_NOT_SUPPORTED_FILE_NOT_FOUND has been removed in 3.6.3.
* In 4.0 an Exception (type to be determined) will be thrown.
* For more info see https://github.com/joomla/joomla-cms/issues/11570
*/
return false;
}
}
Suggested code
// Only try to load the class if it doesn't already exist.
if (!class_exists($tableClass) && !class_exists($tableClassLegacy)) {
// Search for the class file in the JTable include paths.
$paths = self::addIncludePath();
$pathIndex = 0;
while (!class_exists($tableClass) && !class_exists($tableClassLegacy) && $pathIndex < \count($paths)) {
if ($tryThis = Path::find($paths[$pathIndex++], strtolower($type) . '.php')) {
// Import the class file.
include_once $tryThis;
}
}
}
if (!class_exists($tableClass) && class_exists($tableClassLegacy)) {
$tableClass = $tableClassLegacy;
}
if (!class_exists($tableClass)) {
/*
* If unable to find the class file in the Table include paths. Return false.
* The warning JLIB_DATABASE_ERROR_NOT_SUPPORTED_FILE_NOT_FOUND has been removed in 3.6.3.
* In 4.0 an Exception (type to be determined) will be thrown.
* For more info see https://github.com/joomla/joomla-cms/issues/11570
*/
return false;
}
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
Thank you. We will change this approach in the future then.
Another approach on how to create tables - https://forum.joomla.org/viewtopic.php?t=988610
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2023-10-20 08:26:07 |
Closed_By | ⇒ | HLeithner |
Yes normally you would boot the component and ask for the table but some tables are not component bound and can be created directly.
Pull Request #42180 created. Closing this. @georgebara please test the PR and mark it as success (if it works ;-)
Thanks, will create a pr for 5.0.1
btw. if possible don't use JTable::getInstance in the future.