I have custom action (saw in docs as recommended method) that makes some logic and returns doctrine collection of entities.
With regular api-platform action filters working perfectly. But how can i get any from default filters to work with this collection in my custom action?
When i request GET /cars?createdAt[after]=2018-08-01 or GET /drivers?createdAt[after]=2018-08-01 it works as expected.
But when i'm trying to do GET /drivers/42/cars_custom_logic?createdAt[after]=2018-08-01 it doesn't filter anything. It's expected as i didn't call filter in my custom action, but my question is – how to add this filter?
App\Entity\Car<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
 * @ORM\Entity
 * @ApiResource
 * @ApiFilter(DateFilter::class, properties={"createdAt"})
 */
class Car
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"car", "driver"})
     */
    private $id;
    /**
     * @ORM\Column(type="datetime")
     * @Groups({"car", "driver"})
     */
    private $createdAt;
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Driver", inversedBy="cars")
     * @Groups({"car", "driver"})
     */
    private $driver;
    public function __construct()
    {
        $this->createdAt = new \DateTime('now');
    }
    public function getId(): int
    {
        return $this->id;
    }
    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }
    public function getDriver(): Driver
    {
        return $this->driver;
    }
}
App\Entity\Driver<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
 * @ORM\Entity
 * @ApiResource(itemOperations={
 *     "get",
 *     "special"={
 *         "method"="GET",
 *         "path"="/drivers/{id}/cars_custom_logic",
 *         "controller"=GetDriverCarsAction::class
 *     }
 * })
 * @ApiFilter(DateFilter::class, properties={"createdAt"})
 */
class Driver
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"car", "driver"})
     */
    private $id;
    /**
     * @ORM\Column(type="datetime")
     * @Groups({"car", "driver"})
     */
    private $createdAt;
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Car", mappedBy="driver")
     * @Groups({"car", "driver"})
     */
    private $cars;
    public function __construct()
    {
        $this->createdAt = new \DateTime('now');
    }
    public function getId(): int
    {
        return $this->id;
    }
    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }
    /**
     * @return Collection|Car[]
     */
    public function getCars(): Collection
    {
        return $this->cars;
    }
}
App\Controller\GetDriverCarsAction<?php
namespace App\Controller;
use App\Entity\Car;
use App\Entity\Driver;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\RegistryInterface;
final class GetDriverCarsAction
{
    private $doctrine;
    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }
    public function __invoke(Driver $driver): Collection
    {
        $cars = $driver->getCars();
        // ..... Some domain logic .....
        // ..... Here – what should i do to make filter work here? .....
        return $cars;
    }
}
What if you try adding via yaml like this:
# api/config/api_platform/resources.yaml
App\Entity\Book:
    attributes:
       filters: [ offer.date_filter ]
    itemOperations:
        get: ~
        special:
            method: 'GET'
            path: '/books/{id}/special'
            controller: 'App\Controller\BookSpecial'
OR
# api/config/api_platform/resources.yaml
App\Entity\Book:
    itemOperations:
        get: ~
        special:
            method: 'GET'
            path: '/books/{id}/special'
            controller: 'App\Controller\BookSpecial'
            filters: ['offer.date_filter']
For more deep look at this documentation: https://api-platform.com/docs/core/filters#doctrine-orm-filters
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With