如何自动过滤嵌入关系数据?

问题描述 投票:0回答:1

我有两个相互关联的简单实体:

<?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。现在我得到了所有记录。

我尝试使用QueryItemExtensionInterfaceQueryCollectionExtensionInterface,但我仍然获得所有记录。 fetch ="EAGER"也没有帮助我

我澄清,我不想通过子资源/api/categories/{id}/objects来获得它。我想使用/api/сategories/{id}

获得它

我也希望这取决于用户的角色。这样管理员可以获取所有记录,并且只有用户isActive = true

任何想法如何做到这一点?

api-platform.com
1个回答
0
投票

Boolean Filtercategories?objects.isActive=1一起使用。此过滤器必须在关系属性上设置为described here

© www.soinside.com 2019 - 2024. All rights reserved.