Jonas
Jonas

Reputation: 379

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.

Upvotes: 2

Views: 3003

Answers (2)

Christoph Hofmann
Christoph Hofmann

Reputation: 78

on TYPO3 9.x

you need

$className = get_class($this);
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$dataMapper = $objectManager->get(
    \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class
);
$tableName = $dataMapper->getDataMap($className)->getTableName();

Upvotes: 4

Euli
Euli

Reputation: 1143

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.

Upvotes: 7

Related Questions