Sometimes it’s necessary to getĀ tt_content data in a custom extbase extension. Because Typo3 doesn’t provide a model for the tt_content table we have to find a workaround. Extbase table mapping is the solution here.
This approach is quite simpel and three files a need only.
EXT:your_extension/Configuration/TypoScript/Setup.txt
In this file we are defining the model namespace, target table and the mappings of all fields we want to use (fieldname -> model-property)
EXT:your_extension/Classes/Domain/Model/Content.php
The model containing all the getter-methods for accessing the tt_content data.
EXT:your_extension/Classes/Domain/Repository/ContentRepository.php
An empty default repository extending a default extbase repository with optional methods for retrieving data.
Model vendor name and extension names of all code must be altered to yours.
Setup.txt code:
config.tx_extbase { persistence{ enableAutomaticCacheClearing = 1 updateReferenceIndex = 0 classes { LuJa\ContentAutocomplete\Domain\Model\Content { mapping { tableName = tt_content columns { uid.mapOnProperty = uid pid.mapOnProperty = pid sorting.mapOnProperty = sorting CType.mapOnProperty = contentType header.mapOnProperty = header } } } } } }
(columns area has table field on the left and model properties on the right side)
Model\Content.php
<?php namespace LuJa\ContentAutocomplete\Domain\Model; /** * Content */ class Content extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { /** * uid * * @var string */ protected $uid = ''; /** * pid * * @var string */ protected $pid = ''; /** * header * * @var string */ protected $header = ''; /** * sorting * * @var string */ protected $sorting = ''; /** * contentType * * @var string */ protected $contentType = ''; /** * Gets the uid * * @return string $uid */ public function getUid() { return $this->uid; } /** * Gets the pid * * @return string $pid */ public function getPid() { return $this->pid; } /** * Returns the header * * @return string $header */ public function getHeader() { return $this->header; } /** * Sets the header * * @param string $header * @return void */ public function setHeader($header) { $this->header = $header; } /** * Returns the sorting * * @return string $sorting */ public function getSorting() { return $this->sorting; } /** * Sets the sorting * * @param string $sorting * @return void */ public function setSorting($sorting) { $this->sorting = $sorting; } /** * Returns the contentType * * @return string $contentType */ public function getContentType() { return $this->contentType; } /** * Sets the contentType * * @param string $contentType * @return void */ public function setContentType($contentType) { $this->contentType = $contentType; } }
ContentRepository.php
<?php namespace LuJa\ContentAutocomplete\Domain\Repository; /** * The repository for Contents */ class ContentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository { }
Now it’s possible to get all tt_content data inside your controller with the following code:
/** * contentRepository * * @var \LuJa\ContentAutocomplete\Domain\Repository\ContentRepository * @inject */ protected $contentRepository = NULL; /** * action list * * @return void */ public function listAction() { $contents = $this->contentRepository->findAll(); $this->view->assign('contents', $contents); }
Troubleshooting:
- Static Extension Typoscript Template is not included in the main template
- storagePid is not set or wrong (alternatively set setRespectStoragePage to false)