如何通过自定义操作使用默认的API平台过滤器?

时间:2018-08-21 10:47:09

标签: php symfony api-platform.com

我有一个自定义动作(saw in docs as recommended method),该动作会产生一些逻辑并返回实体的学说集合。

使用常规的api平台动作过滤器可以完美运行。但是,如何在我的自定义操作中让any from default filters与该收藏集一起使用?

当我请求GET /cars?createdAt[after]=2018-08-01GET /drivers?createdAt[after]=2018-08-01时,它会按预期工作。

但是当我尝试执行GET /drivers/42/cars_custom_logic?createdAt[after]=2018-08-01时,它不会过滤任何内容。可以预期,因为我没有在自定义操作中调用过滤器,但是我的问题是 –如何添加此过滤器?


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;
    }
}

1 个答案:

答案 0 :(得分:1)

如果您尝试通过yaml这样添加怎么办:

# 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']

要深入了解此文档,请访问:https://api-platform.com/docs/core/filters#doctrine-orm-filters

希望有帮助