My virtual field is not showing with API Platform on Symfony 4

I would like to add a custom field in my entity normalization for API Platform but it won’t show up..

Desired behaviour: when json is returned, there would be a new field called ServerDomain

I red that I just had to add a simple getter with a groups annotation, but it won’t work.

My APIResource decoration of my entity:


/**
 * @ApiResource(
 *   normalizationContext={"groups"={"read"}},
 *   denormalizationContext={"groups"={"write"}},
 *   itemOperations={
 *     "get", "put", "delete", "patch"
 *   }
 * )
 * @ORMEntity(repositoryClass=ServerRepository::class)
 */

My custom getter:

    /**
     * @Groups({"read"})
     */
    public function getServerDomain(): ?string
    {
        return $this->getServerSubDomain() . ".example.com";
    }

I only have one entity for now.
I have a use for Groups Annotations and it works fine for the ORM properties.

I’m using php 7.4.10 and Symfony 4.2

Thanks for your answers !

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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