Order on three level ApiSubressource

With ApiPlatform and Symfony5

I have three entities:

  • EntityOne
  • EntityTwo
  • EntityThree

I’m querying EntityOne, so I get embedded data from EntityTwo and then EntityThree thanks to @ApiSubressource() annotation.

The trick part is that I want to order EntityTwo and EntityThree by their starting date, so the front end can directly work with those data without adding more lines.

I’ve used ApiPlatform "order" property (https://api-platform.com/docs/core/default-order/).

It work on one depth level, so for EntityTwo.

I’ve try to implement the same logic for entityThree but for some reasons it’s not picking up.

Which made me think that maybe those kind of ordering does not work fore more than one depth ?

If anyone has any more informations on that I’d be gratefull, as the doc seems to not speak abt that, except if I’m missing something.

EntityOne:

/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"entityone:read", "entityone:list"}},
 *          "denormalization_context"={"groups"={"entityone:write"}},
 *          "order"={"entitytwo.start": "ASC"}
 *     },
 *
 */

class EntityOne
{

    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"entityone:read", "entityone:list"})
     */
    private $id;
    /**
     * @ORMOneToMany(targetEntity=entitytwo::class, mappedBy="entityone", cascade={"remove"})
     * @Groups({"entityone:read", "entityone:list", "entityone:write"})
     * @ApiSubresource()
     */
    private $entitytwo;

EntityTwo:

/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"entitytwo:read", "entitytwo:list"}},
 *          "denormalization_context"={"groups"={"entitytwo:write"}},
 *          "order"={"entitythree.debut": "ASC"}
 *     },
 */
class EntityTwo
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"entitytwo:read", "entitytwo:list", "entityone:read", "entityone:list"})
     */
    private $id;

    /**
     * @ORMColumn(type="datetime")
     * @Groups({"entitytwo:read", "entitytwo:list", "entitytwo:write", "entityone:read", "entityone:list"})
     */
    private $start;

    /**
     * @ORMOneToMany(targetEntity=entitythreee::class, mappedBy="entitytwo", orphanRemoval=true)
     * @Groups({"entitytwo:read", "entitytwo:list", "entityone:list", "entityone:read"})
     * @ApiSubresource()
     */
    private $entitythree;

EntityThree:

/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"entitythree:read", "entitythree:list"}},
 *          "denormalization_context"={"groups"={"entitythree:write"}},
 *          "order"={"debut": "ASC"}
 *     }
 */
class EntityThree
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"entitythree:read", "entitythree:list", "entitytwo:read", "entitytwo:list", "entityone:read", "entityone:list"})
     */
    private $id;

    /**
     * @ORMColumn(type="datetime")
     * @Groups({"entitythree:read", "entitythree:list", "entitythree:write", "entitytwo:read", "entitytwo:list", "entityone:read", "entityone:list"})
     */
    private $debut;

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *