我有带有过滤器的Entity。我使用SearchFilter并一切正常,但现在我添加了OrderFilter和NumericFilter,此过滤器不起作用。我没有在文档中提到以下事实:对于这些过滤器,您需要在配置中指定某些内容。为什么不起作用,我不知道。
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Gedmo\Mapping\Annotation as Gedmo;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\NumericFilter;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductWatcherRepository")
* @ORM\HasLifecycleCallbacks()
*
* @ApiResource(
* routePrefix="/profile",
* attributes={
* "normalization_context"={"groups"={"product-watcher-list"}},
* },
* itemOperations={"get"={"method"="GET"}},
* collectionOperations={"get"={"method"="GET"}},
* )
*
* @ApiFilter(NumericFilter::class, properties={"percent"}) // NOT WORK
* @ApiFilter(OrderFilter::class, properties={"createdAt", "title"}) // NOT WORK
* @ApiFilter(SearchFilter::class, properties={"title": "partial"}) // WORK
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true)
*/
class ProductWatcher
{
const STATUS_NEW = 1; // New Product Watcher
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups("product-watcher-list")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Groups("product-watcher-list")
*/
private $title = '';
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
* @Assert\Range(
* min = 1,
* max = 99,
* minMessage = "v.percent.range",
* maxMessage = "v.percent.range"
* )
* @Groups("product-watcher-list")
*/
private $percent = 0;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
private $createdAt = 0;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="productWatchers")
* @ORM\JoinColumn(nullable=true)
* @Groups("product-watcher-list")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="productWatchers")
* @ORM\JoinColumn(nullable=true)
* @ApiSubresource
* @Groups("product-watcher-list")
*/
private $product;
............
仅SearchFilter起作用,其余部分不起作用,可能也是问题,大张旗鼓,只有SearchFilter的字段可见
工作:
/api/profile/product_watchers?title=product1
不起作用:
/api/profile/product_watchers?percent=20
/api/profile/product_watchers?order[title]=desc
我已经不知道为什么不起作用了。我没有缓存,我重新启动了环境。