Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the table name from model class name in TYPO3 extbase?

What is the best way to get the table name from a specific object? Is there something like:

$tableName = Utility::doSomeMagic($object);

So that you get tx_extkey_domain_model_myobject from Vendor\Extkey\Domain\Model\MyObject.

like image 520
Jonas Avatar asked Jan 20 '26 17:01

Jonas


1 Answers

You can use the DataMapper to get the table name of a model. It is used internally by the Repositories(indirectly at least) to tell what they are dealing with. You can get yourself an instance of the DataMapper and use it like so:

$className = \MyVendor\MyExt\Domain\Model\SomeModel::class;
$dataMapper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
$tableName = $dataMapper->getDataMap($className)->getTableName();

Take a look at the SqlDebuggerUtility from this github repository, which is using the DataMapper to get the tablename of a QueryResult object to debug the SQL statements.

like image 71
Euli Avatar answered Jan 23 '26 20:01

Euli