简体   繁体   English

TYPO3 / Extbase:数据映射器不再适用于 TYPO3 9.5.x

[英]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.在 TYPO3 8.7.x 等较旧的 TYPO3 版本中,我使用 DataMapper 将查询生成器选择结果的结果映射到对象数组。 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 ".这在 TYPO3 8.7.x 中工作正常,但在 TYPO3 9.5.x 中,我收到错误消息“ 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.如果属性用@inject注释或者有匹配的injectPropertyName方法,则在TYPO3 中就是这种情况。

In that case, you should instantiate the class (DataMapper in this case) using the ObjectManager.在这种情况下,您应该使用 ObjectManager 实例化类(在本例中为 DataMapper)。

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由于 ObjectManager 的get方法在 TYPO3 版本 10 中被标记为已弃用,因此我使用inject注释来获取 DataMapper 实例

    /**
     * Datamaper
     *
     * @var DataMapper
     * @TYPO3\CMS\Extbase\Annotation\Inject
     */
    protected $dataMapper;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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