#101125Ability to convert Domain Models to Array
Description
In the same context with this https://forge.typo3.org/issues/101124, it would be very practical if there was a function in the AbstractEntity , which converts the Model to array.
Technology is moving fast, and headless and communication between systems are made mostly with json strings. Currently, one has to create a method in the domain model which return the values as arrays, but this also could be PITA if an extension has 10+ models and relations. Specially now that with TYPO3 12, we have Webhooks and Reactions , which makes the communication between systems very easy.
*Solution*
Create a method in AbstractEntity which processes everything (FileReferences, Relations) and return everything as an array
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
Fix this issue
Start the commit message of your patch with this. Add what the patch does, and the versions it goes to in "Releases:". How to create a patch
- Commit message
-
[FEATURE] Ability to convert Domain Models to Array Resolves: #101125 Releases: main
Related issues 1
History 5
-
Benni Mack changed Related to #101124 <span data-processor="relativetime" data-value="2023-07-03T16:23:08+00:00" > 07/03/23, 16:23 </span>
-
Stefan Froemken commented <span data-processor="relativetime" data-value="2023-07-05T05:47:33+00:00" > 07/05/23, 05:47 </span>
-
Aristeidis Karavas commented <span data-processor="relativetime" data-value="2023-10-17T08:54:35+00:00" > 10/17/23, 08:54 </span>
Hallo Stefan,
calling another class to modify the current called Model, i think it is too much. The same could be achieved by not calling the model at all and just make a QueryBuilder request with all the joins. A more suitable approach would be a function inside the model which returns everything as array, so the developer could just do something like that: $object = $this->someRepository->findByUid()->toArray
Not saying that the current implementation it is not a solution, but the other one would be more practical and understandable, specially when it comes to documentation
If it is too much, or not possible with the current state of TYPO3, sure, creating a patch for the ObjectAccess would be ok
Aris
-
Simon Schaufelberger commented <span data-processor="relativetime" data-value="2026-02-14T00:47:18+00:00" > 02/14/26, 00:47 </span>
This is how Laravel does it: https://github.com/laravel/framework/blob/987a14011610aeb77826d7b95c3055ac330f0c85/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L215
Also good to read: https://symfony.com/doc/current/serializer.html
-
Garvin Hicking commented <span data-processor="relativetime" data-value="2026-02-14T08:37:45+00:00" > 02/14/26, 08:37 </span>
The problem isn’t to put all getable properties in an array with their keys. The problem is that domain logic may return arbitrary objects with arbitrary sub objects in them. Some will be FileRefetence and ObjectStorage of course, but any custom object may be contained. How to serialize a generic object? TYPO3s extbase model logic is very free and not constrained by interfaces that could convert an object i lnto non-object structures.
So I’m afraid this would first require stable domain model contracts, and an interface each object and EACH getter return method would need to implement if it’s not a scalar.
So for now I recommend to implement a custom domain model method “_asArray()” and the manually iterate your known properties and to string serializing as needed.
Hello Karavas,
You can use \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGetableProperties($object); to do so in your controller.
I know, that this method needs a further method to do its job recursive. Maybe I can create a patch for that.
Would that be an option for you?
Stefan