简体   繁体   中英

TYPO3 / Extbase: Datamapper not working anymore with TYPO3 9.5.x

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM