Search code examples
apitypo3domain-driven-designextbase

TYPO3 - Extbase - Map foreign data (provided by API)


I want to use foreign data (provided by an external API) within an extbase extension. The data should be accessible by any kind of repository, providing the foreign objects as local models. There should not be any local database persistence layer for the foreign data in between.

Let’s take a „blog“ with posts as an example: I could implement a custom „PostRepository“ with „findAll()“ and „findByUid()“ methods, which query the API in behind and map the retrieved objects to local domain objects. But how can i use the benefits of extbase object mapping mechanism? I would like to be able to do something like:

PostController::showAction(Post $post)

What is necessary to get the argument mapping mechanism work for this (not locally persisted) data? Is this possible at all? Can anybody point me into the right direction?


Solution

  • For mapping data to models, TYPO3 has its DataMapper.

    A piece of explanation can be found in the docs of blog-example:

    The DataMapper object has the task to create an instance of the objects class for each tuple and “fill” this fresh instance with the data of the tuple. The DataMapper object also resolves all relations.

    To make it less abstract, a bit of code:

    public function mapProperties(): AnyModel
    {
      $properties = [
        'uid' => 123,
        'pid' => 123,
        'firstname' => 'Alex',
        'lastname' => 'Kellner'
        'email' => 'my@email.org'
      ];
      $dataMapper = $this->objectManager->get(TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
      $anyModels = $dataMapper->map(AnyModel::class, [$properties]);
      return $anyModels[0];
    }
    

    (Source: https://gist.github.com/einpraegsam/679156ccd9fb1cb94af5f901519d89fc)