Save order of custom entities in selection field

Let’s say I have two custom entities:

  • Dish
  • Ingredient

The entities have a many-to-many relation with extra field: ingredient_position:

#[ORMEntity]
class Dish
{
    #[ORMOneToMany(
        mappedBy: 'dish',
        targetEntity: DishIngredient::class,
        cascade: ['all'],
    )]
    private Collection $dishIngredients;
}

#[ORMEntity]
class Ingredient
{
    #[ORMOneToMany(
        mappedBy: 'ingredient',
        targetEntity: DishIngredient::class,
        cascade: ['all'],
    )]
    private Collection $dishIngredients;
}

#[ORMEntity]
class DishIngredient
{
    public function __construct(
        #[
            ORMManyToOne(
                targetEntity: Dish::class,
            ),
            ORMJoinColumn(nullable: false),
        ]
        private Dish $dish,
        #[
            ORMManyToOne(
                targetEntity: Ingredient::class,
            ),
            ORMJoinColumn(nullable: false),
        ]
        private Ingredient $ingredient,
        
        #[ORMColumn(type: Types::INTEGER)]
        private int $ingredientPosition = 0,
    ) {}
}

I’d like to be able to add existing Ingredients when adding/editing Dish but the ingredients have to be stored in the order I set in the admin form.

I tried to set up a ingredient_selection field in sulu_admin and it works, when I select ingredients and store them in the database, but when I load the Dish in the admin form, the ingredients don’t show up – Sulu tries to load ingredients from the ingredients’ endpoint: /admin/api/ingredients?locale=en&ids=9,10&page=1&flat=true: in that case the ids are the IDs of the DishIngredient entity, but I’m not able to load the ingredients.