我有两个相互关联的简单实体:
<?php
declare(strict_types=1);
namespace App\Entity;
// use ... ;
/**
* @ApiResource(
* subresourceOperations={
* // ...
* },
* // ... ,
* normalizationContext={"groups" = {"category:read"}}
* )
* @ ...
*/
class Category
{
// ...
/**
* @ORM\OneToMany(targetEntity="Object", mappedBy="category")
* @ApiSubresource
* @Groups({"category:read"})
*/
private $objects;
// ...
public function getObjects(): Collection
{
return $this->objects;
}
}
和
<?php
declare(strict_types=1);
namespace App\Entity;
// use ... ;
/**
* @ApiResource(
* // ...
* )
* @ ...
*/
class Object
{
// ...
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
* @ORM\JoinColumn(nullable=false)
* @Groups({"object:read", "object:write"})
*/
private $category;
/**
* @ORM\Column(type="boolean")
*/
private $isActive = true;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"object:read", "object:write", "category:read"})
*/
private $name;
// ...
}
我想通过/api/сategories/{id}
获得相关数据Objects
,其中isActive = true
。现在我得到了所有记录。
我尝试使用QueryItemExtensionInterface
和QueryCollectionExtensionInterface
,但我仍然获得所有记录。 fetch ="EAGER"
也没有帮助我
我澄清,我不想通过子资源/api/categories/{id}/objects
来获得它。我想使用/api/сategories/{id}
我也希望这取决于用户的角色。这样管理员可以获取所有记录,并且只有用户isActive = true
任何想法如何做到这一点?
将Boolean Filter与categories?objects.isActive=1
一起使用。此过滤器必须在关系属性上设置为described here。