TYPO3 developers are often faced with the same requirement: Import data into TYPO3. TYPO3 itself offers many ways to import data into the system. The DataHandler (TCE) or Extbase might come to your mind, as well as plain Doctrine DBAL queries. This post will explain my views on each of the options and their pros and cons. I don't consider any of them the general solution, but the pros and cons might help you find the one suiting your current use case best.
I always would suggest DataHandler first for the following reasons:
- API is pretty simple, build a single array and submit.
- It handles everything you can think of, and everything you don't remember:
- Hooks (indexing in solr, or whatever your system uses hooks for)
- Cache flushing
- … (things I forgot)
But there are some downsides:
- It is slow (You can tweak the DataHandler instance to skip some things and be more performant)
- It might do more than you want, e.g. history, logging, relations, permissions, hooks, cache flushing, ….
Doctrine DBAL ¶
I than would recommend Doctrine DBAL for the following reasons:
- You are in full control. You can do exactly what you want the way you want, and can optimize for memory and performance.
But it also has downsides:
- You need to remember all necessary steps and rebuild integrations, e.g. indexing into solr, triggering cache flushes, etc.
And then there is Extbase for the following reasons:
- You stay in OOP
- Most developers are probably more familiar with Extbase than the other APIs
- It was not build for that task.
- You need to add workarounds to keep performance okay.
- You still need to do extra work like triggering indexing, cache flushing, etc.
It depends ¶
I would say it depends on:
- What do you need from the system, do you need hooks, logging (history) and cache flushing?
- Do you know what you do and want to have full control over every aspect?
- How much data do you have, how much memory and time do you have?
- Do you want to keep OOP?
This is a recurring topic, but Eric Harrer finally made me write this blog post by bringing up the topic once more at the Fediverse: https://phpc.social/@ErHaWeb/111096230843826340.
Read more about:
- TCE (TYPO3 Core engine) & DataHandler: https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Typo3CoreEngine/Index.html
- Doctrine DBAL: https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Database/Index.html
- Example Extension importing JSON data mapped to Objects using DataHandler in the end: https://git.daniel-siepmann.de/Customers/thuecat / https://github.com/werkraum-media/thuecat
- Example Extension importing JSON data mapped to Extbase Entities using mostly Extbase for importing: https://git.daniel-siepmann.de/Customers/events / https://github.com/werkraum-media/events