Hello guys I just started using Api-Platform and I got stuck on this problem for several hours.
I have a Symfony4 Project and two Entities: Bill & Abo
Bill:
/**
* @ORM\Entity(repositoryClass="App\Repository\BillRepository")
* @ApiResource(attributes={
* "normalization_context"={"groups"={"bill-abo"}}
* })
*/
class Bill {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date", nullable=false)
* @Groups("bill-abo")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Abo", inversedBy="bills")
* @ORM\JoinColumn
* @ApiSubresource
* @Groups("bill-abo")
*/
private $abo;
}
Abo:
/**
* @ORM\Entity(repositoryClass="App\Repository\AboRepository")
* @ApiResource
*/
class Abo
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50, nullable=false)
*/
private $name;
/**
* @var integer
*
* @ORM\Column(name="price", type="integer", nullable=false)
* @Groups("bill-abo")
*/
private $price;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Bill", mappedBy="abo")
*/
private $bills;
}
When I now call this url, /api/bills
, I get back this data:
{
"date": "2018-03-14T00:00:00+00:00",
"abo": {
"price": 960
}
},
...
My goal now is to call an url, something like this: /api/bills?abo
to get the result above and when I call /api/bills
I only want the bill data (without the abo data), like this:
{
"id": 14,
"date": "2018-03-08T00:00:00+00:00",
"abo": "/api/abos/1"
},
...
I read the docs and specifically the parts about the Group Filter and the Property Filter but I coulnd't figure out a solution.
Is this even possible and if yes how can I achieve this?
Thanks for your time and help!
You can leverage the Group Filter.
Bill
resource class like this @ApiFilter(GroupFilter::class, arguments={"overrideDefaultGroups": true, "whitelist": {"foo"}})
Bill::$abo
property (don't add to any property in the Abo
class)/bills?groups[]=foo