Handling Cascade Persistence 

Create relation to existing entity 

It is super easy to create relation to already existing entity. To do so you just need to pass uid of the related object as a value of property. It works in exactly same way for One-To-One and Many-To-One relations for whom target entity contains information about one related entity. As on example below - we want to assign User to Department assuming that User can belong to only one Department.

{
   "department": 12
}
Copied!

In similar way it works also for One-To-Many and Many-To-Many relations for whom target entity contains a collection of related entities. In such case to create relation it is needed to pass array of identifiers. In example below we create/update news and assign it to categories with specified identifiers.

{
   "categories": [
      10,
      12,
      13
   ]
}
Copied!

Relations stored in the related table 

Records of relations stored in the related table - TCA inline with foreign_field and TCA file (file references) - keep their parent record in their own row and belong to this parent only. An existing record of such relation can be passed by uid (also to update it, see below) only when it already belongs to the same property of the same record, e.g. when sending the current items again in PATCH or PUT. Otherwise the response is 400 Bad Request. New items are created in the same request - pass them without uid using cascade persistence described below, file references with uidLocal (see Handling File Upload).

{
   "steps": [
      1,
      {"uid": 2, "title": "Changed title of existing step"},
      {"title": "New step"}
   ]
}
Copied!

The collection sent in the request replaces the current one. Extbase does not delete an item removed from the collection, it only clears its relation to the parent. To delete removed items, add the Extbase Cascade attribute to the property. TYPO3 14 reads Extbase annotations only from PHP attributes, not from doc comments. The form below works in TYPO3 12, 13 and 14 (in TYPO3 14 you can use #[\TYPO3\CMS\Extbase\Attribute\ORM\Cascade('remove')] instead):

use SourceBroker\T3api\Annotation as T3api;
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Ext\Domain\Model\Step>
 * @T3api\ORM\Cascade("persist")
 */
#[Cascade(['value' => 'remove'])]
protected ObjectStorage $steps;
Copied!

An existing record which can not be found (also a hidden one) returns 400 Bad Request. A record passed with "uid": 0 is created as a new one. Value 0 clears a single relation; in a collection it returns 400 Bad Request.

If records of your relations are owned by users and should be checked by their owner, add a rule for it in security_post_denormalize of the operation, e.g. a method of the model which checks all items, see Security.