No Code Attached Yet
avatar georgebara
georgebara
20 Oct 2023

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

Function Location

1 () JROOT\libraries\src\Table\Table.php:344

Steps to reproduce the issue

Call JTable::getInstance($type, $prefix) twice for a table that has the JTable prefix (it works for tables with Table prefix)

Expected result

Retrieve the table object

Actual result

Throws an error

System information (as much as possible)

Joomla 5.0

Additional comments

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;
}

Votes

# of Users Experiencing Issue
1/1
Average Importance Score
5.00

avatar georgebara georgebara - open - 20 Oct 2023
avatar georgebara georgebara - change - 20 Oct 2023
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 20 Oct 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 20 Oct 2023
avatar georgebara georgebara - change - 20 Oct 2023
The description was changed
avatar georgebara georgebara - edited - 20 Oct 2023
avatar georgebara georgebara - change - 20 Oct 2023
The description was changed
avatar georgebara georgebara - edited - 20 Oct 2023
avatar HLeithner HLeithner - change - 20 Oct 2023
The description was changed
avatar HLeithner HLeithner - edited - 20 Oct 2023
avatar georgebara georgebara - change - 20 Oct 2023
The description was changed
avatar georgebara georgebara - edited - 20 Oct 2023
avatar HLeithner
HLeithner - comment - 20 Oct 2023

Thanks, will create a pr for 5.0.1

btw. if possible don't use JTable::getInstance in the future.

avatar HLeithner HLeithner - change - 20 Oct 2023
The description was changed
avatar HLeithner HLeithner - edited - 20 Oct 2023
avatar georgebara
georgebara - comment - 20 Oct 2023

Thank you. We will change this approach in the future then.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42179.

avatar georgebara
georgebara - comment - 20 Oct 2023

Another approach on how to create tables - https://forum.joomla.org/viewtopic.php?t=988610


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42179.

avatar HLeithner HLeithner - change - 20 Oct 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-10-20 08:26:07
Closed_By HLeithner
avatar HLeithner HLeithner - close - 20 Oct 2023
avatar HLeithner
HLeithner - comment - 20 Oct 2023

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 ;-)

Add a Comment

Login with GitHub to post a comment