Reputation: 866
In older TYPO3 Versions like TYPO3 8.7.x, I used DataMapper to map the results from my querybuilder select result to an array of objects. That is working fine in TYPO3 8.7.x, but in TYPO3 9.5.x, I've got the error message "Call to a member function buildDataMap() on null".
//MyRepository.php
namespace Vendor\MyExtension\Domain\Repository;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
/**
* @param string $search
*
* @return array
*/
public function findBySearch($search)
{
$querybuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myextension_domain_model_produkt');
$records = $querybuilder->select('tx_myextension_domain_model_produkt.*')
->from('tx_myextension_domain_model_produkt')
->orWhere(
$querybuilder->expr()->like('titel', $querybuilder->createNamedParameter('%' . $search . '%')),
$querybuilder->expr()->like('untertitel', $querybuilder->createNamedParameter('%' . $search . '%'))
)
->orderBy('titel')
->execute()
->fetchAll();
$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
return $dataMapper->map($this->objectType, $records);
}
Upvotes: 0
Views: 2147
Reputation: 1618
Since the get
method of ObjectManager is marked as deprecated in TYPO3 version 10 I use inject
annontation to get DataMapper instance
/**
* Datamaper
*
* @var DataMapper
* @TYPO3\CMS\Extbase\Annotation\Inject
*/
protected $dataMapper;
Upvotes: 0
Reputation: 789
Some classes require other objects as dependencies. This is the case in TYPO3 if the properties are annotated with @inject
or if there is a matching injectPropertyName
method.
In that case, you should instantiate the class (DataMapper in this case) using the ObjectManager.
That usually looks like this:
$dataMapper = GeneralUtiity::makeInstance(ObjectManager::class)->get(DataMapper::class);
Upvotes: 4