具有uuid的API平台过滤器

时间:2018-10-26 05:19:00

标签: filter doctrine symfony4 api-platform.com

Okey,所以我最近建立了一个项目,在该项目中,我根据https://api-platform.com/docs/core/identifiers/将api平台与ramsey / uuid-dictionrine相关联。

基本的CRUD东西都可以用,但是我在ApiFilter上得到了意外的行为。

基本设置是这样,我有平台对象,这些平台对象又包含都在astract类上进行中继的组织。

摘要

class AbstaractEntity 
{   
    /**
     * @var \Ramsey\Uuid\UuidInterface
     *
     * @ORM\Id
     * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
     * 
     * @Groups({"read", "write"})
     * @ApiProperty(iri="https://schema.org/identifier")
     */
    protected $id;

 * The Platform to wich this object belongs.
 * 
 * @ORM\ManyToOne(targetEntity="App\Conduction\PlatformBundle\Entity\Platform")
 * @ORM\JoinColumn(name="platform_id", referencedColumnName="id")
 * @Groups({"read", "write"})
 * @MaxDepth(1)
 * @ApiProperty()
 */
protected $platform;

...
}

组织

/**
 * Organisation entity
 *
 * @ORM\Table(
 *     "organisation__organisation"
 * )
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\EntityListeners({"App\Conduction\OrganisationBundle\EventListener\OrganisationListener","App\Conduction\AppBundle\EventListener\EntityListener"})
 * @ApiResource(attributes={
 *     "normalization_context"={"groups"={"read"}, "enable_max_depth"="true"},
 *     "denormalization_context"={"groups"={"write"}}
 * })
 * @ApiFilter(SearchFilter::class, properties={"id": "exact", "description": "partial", "name": "partial","organisation": "exact","platform": "exact"})
 * @ApiFilter(DateFilter::class, properties={"dateCreated","datePublished","dateModified","dateDeleted"})
 * @ApiFilter(OrderFilter::class, properties={"id", "name","dateCreated","datePublished","dateModified","dateDeleted"}, arguments={"orderParameterName"="order"})
 * @Gedmo\SoftDeleteable(
 *     fieldName = "dateDeleted",
 *     timeAware = true
 * )
 */
class Organisation extends \App\Conduction\AppBundle\Entity\AbstaractEntity
{
....
}

因此所有基础知识都井井有条,并且api /组织给出了以下要求

{
    "@context": "/api/contexts/Organisation",
    "@id": "/api/organisations",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/organisations/ba8dc018-d8d5-11e8-bcb6-5254007d3b24",
            "@type": "Organisation",
            "slug": null,
            "organisation": "/api/organisations/ba8dc018-d8d5-11e8-bcb6-5254007d3b24",
            "children": [
                "/api/organisations/ba8dc018-d8d5-11e8-bcb6-5254007d3b24"
            ],
            "id": "ba8dc018-d8d5-11e8-bcb6-5254007d3b24",
            "name": "Conduction",
            "description": "This is the defeault platform",
            "platform": {
                "@id": "/api/platforms/ba8d9958-d8d5-11e8-a9a5-5254007d3b24",
                "@type": "Platform",
                "approveUsers": true,
                "validateUsers": true,
                "organisation": "/api/organisations/ba8dc018-d8d5-11e8-bcb6-5254007d3b24",
                "id": "ba8d9958-d8d5-11e8-a9a5-5254007d3b24",
                "name": "Conduction",
                "description": "This is the defeault platform",
                "platform": "/api/platforms/ba8d9958-d8d5-11e8-a9a5-5254007d3b24",
                "dateCreated": "2018-10-26T06:15:09+02:00",
                "datePublished": "2018-10-26T06:15:09+02:00",
                "dateModified": "2018-10-26T06:15:10+02:00"
            },
            "dateCreated": "2018-10-26T06:15:09+02:00",
            "datePublished": "2018-10-26T06:15:09+02:00",
            "dateModified": "2018-10-26T06:15:10+02:00"
        },
        {
            "@id": "/api/organisations/5f87e080-d8d6-11e8-9809-5254007d3b24",
            "@type": "Organisation",
            "slug": null,
            "organisation": "/api/organisations/5f87e080-d8d6-11e8-9809-5254007d3b24",
            "children": [
                "/api/organisations/5f87e080-d8d6-11e8-9809-5254007d3b24"
            ],
            "id": "5f87e080-d8d6-11e8-9809-5254007d3b24",
            "name": "test 1",
            "description": "<p>test 1&nbsp;</p>",
            "platform": null,
            "dateCreated": "2018-10-26T06:19:47+02:00",
            "datePublished": "2018-10-26T06:19:47+02:00",
            "dateModified": "2018-10-26T06:19:47+02:00"
        },
        {
            "@id": "/api/organisations/f2079af4-d8d6-11e8-ba9a-5254007d3b24",
            "@type": "Organisation",
            "slug": null,
            "organisation": "/api/organisations/f2079af4-d8d6-11e8-ba9a-5254007d3b24",
            "children": [
                "/api/organisations/f2079af4-d8d6-11e8-ba9a-5254007d3b24"
            ],
            "id": "f2079af4-d8d6-11e8-ba9a-5254007d3b24",
            "name": "test 1",
            "description": "<p>test 1</p>",
            "platform": {
                "@id": "/api/platforms/f20723bc-d8d6-11e8-8ffb-5254007d3b24",
                "@type": "Platform",
                "approveUsers": true,
                "validateUsers": true,
                "organisation": "/api/organisations/f2079af4-d8d6-11e8-ba9a-5254007d3b24",
                "id": "f20723bc-d8d6-11e8-8ffb-5254007d3b24",
                "name": "test 1",
                "description": "<p>test 1</p>",
                "platform": "/api/platforms/f20723bc-d8d6-11e8-8ffb-5254007d3b24",
                "dateCreated": "2018-10-26T06:23:52+02:00",
                "datePublished": "2018-10-26T06:23:52+02:00",
                "dateModified": "2018-10-26T06:23:52+02:00"
            },
            "dateCreated": "2018-10-26T06:23:52+02:00",
            "datePublished": "2018-10-26T06:23:52+02:00",
            "dateModified": "2018-10-26T06:23:52+02:00"
        }
    ],
    "hydra:totalItems": 3,
    "hydra:search": {
        "@type": "hydra:IriTemplate",
        "hydra:template": "/api/organisations{?id,id[],description,name,organisation,organisation[],platform,platform[],dateCreated[before],dateCreated[strictly_before],dateCreated[after],dateCreated[strictly_after],datePublished[before],datePublished[strictly_before],datePublished[after],datePublished[strictly_after],dateModified[before],dateModified[strictly_before],dateModified[after],dateModified[strictly_after],dateDeleted[before],dateDeleted[strictly_before],dateDeleted[after],dateDeleted[strictly_after],order[id],order[name],order[dateCreated],order[datePublished],order[dateModified],order[dateDeleted]}",
        "hydra:variableRepresentation": "BasicRepresentation",
        "hydra:mapping": [
            {
                "@type": "IriTemplateMapping",
                "variable": "id",
                "property": "id",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "id[]",
                "property": "id",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "description",
                "property": "description",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "name",
                "property": "name",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "organisation",
                "property": "organisation",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "organisation[]",
                "property": "organisation",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "platform",
                "property": "platform",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "platform[]",
                "property": "platform",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateCreated[before]",
                "property": "dateCreated",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateCreated[strictly_before]",
                "property": "dateCreated",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateCreated[after]",
                "property": "dateCreated",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateCreated[strictly_after]",
                "property": "dateCreated",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "datePublished[before]",
                "property": "datePublished",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "datePublished[strictly_before]",
                "property": "datePublished",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "datePublished[after]",
                "property": "datePublished",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "datePublished[strictly_after]",
                "property": "datePublished",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateModified[before]",
                "property": "dateModified",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateModified[strictly_before]",
                "property": "dateModified",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateModified[after]",
                "property": "dateModified",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateModified[strictly_after]",
                "property": "dateModified",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateDeleted[before]",
                "property": "dateDeleted",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateDeleted[strictly_before]",
                "property": "dateDeleted",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateDeleted[after]",
                "property": "dateDeleted",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "dateDeleted[strictly_after]",
                "property": "dateDeleted",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[id]",
                "property": "id",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[name]",
                "property": "name",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[dateCreated]",
                "property": "dateCreated",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[datePublished]",
                "property": "datePublished",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[dateModified]",
                "property": "dateModified",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "order[dateDeleted]",
                "property": "dateDeleted",
                "required": false
            }
        ]
    }
}
然后,人们会想像一下api / organisations?platform = ba8d9958-d8d5-11e8-a9a5-5254007d3b24使所有组织都对该平台有所了解,但没有。它给

{
    "@context": "/api/contexts/Organisation",
    "@id": "/api/organisations",
    "@type": "hydra:Collection",
    "hydra:member": [],
    "hydra:totalItems": 0,
    "hydra:view": {
        "@id": "/api/organisations?platform=ba8d9958-d8d5-11e8-a9a5-5254007d3b24",
        "@type": "hydra:PartialCollectionView"
    },
    "hydra:search": {
        "@type": "hydra:IriTemplate",
        "hydra:template": "/api/organisations{?id,id[],description,name,organisation,organisation[],platform,platform[],dateCreated[before],dateCreated[strictly_before],dateCreated[after],dateCreated[strictly_after],datePublished[before],datePublished[strictly_before],datePublished[after],datePublished[strictly_after],dateModified[before],dateModified[strictly_before],dateModified[after],dateModified[strictly_after],dateDeleted[before],dateDeleted[strictly_before],dateDeleted[after],dateDeleted[strictly_after],order[id],order[name],order[dateCreated],order[datePublished],order[dateModified],order[dateDeleted]}",
        "hydra:variableRepresentation": "BasicRepresentation",
        "hydra:mapping": [
            {
                "@type": "IriTemplateMapping",
                "variable": "id",
                "property": "id",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "id[]",
                "property": "id",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "description",
                "property": "description",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "name",
                "property": "name",
                "required": false
            },
            {
                "@type": "IriTemplateMapping",
                "variable": "organisation",
                "property": "organisation",
                "required": false
            },
            {
            "@type": "IriTemplateMapping",
            "variable": "organisation[]",
            "property": "organisation",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "platform",
            "property": "platform",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "platform[]",
            "property": "platform",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateCreated[before]",
            "property": "dateCreated",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateCreated[strictly_before]",
            "property": "dateCreated",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateCreated[after]",
            "property": "dateCreated",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateCreated[strictly_after]",
            "property": "dateCreated",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "datePublished[before]",
            "property": "datePublished",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "datePublished[strictly_before]",
            "property": "datePublished",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "datePublished[after]",
            "property": "datePublished",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "datePublished[strictly_after]",
            "property": "datePublished",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateModified[before]",
            "property": "dateModified",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateModified[strictly_before]",
            "property": "dateModified",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateModified[after]",
            "property": "dateModified",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateModified[strictly_after]",
            "property": "dateModified",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateDeleted[before]",
            "property": "dateDeleted",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateDeleted[strictly_before]",
            "property": "dateDeleted",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateDeleted[after]",
            "property": "dateDeleted",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "dateDeleted[strictly_after]",
            "property": "dateDeleted",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[id]",
            "property": "id",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[name]",
            "property": "name",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[dateCreated]",
            "property": "dateCreated",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[datePublished]",
            "property": "datePublished",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[dateModified]",
            "property": "dateModified",
            "required": false
        },
        {
            "@type": "IriTemplateMapping",
            "variable": "order[dateDeleted]",
            "property": "dateDeleted",
            "required": false
        }
    ]
}

}

或者简单地将零组织。现在出于寻找错误的目的,我再次构建了整个组件,但现在使用了增量ID,然后它就像一个符咒一样工作。因此,问题似乎出在ApiFilter的工作方式上。这就暗示了ApiFilter实际上并没有在搜索中使用UUID之前对其进行规范化。....

-更新-

Okey,所以我尝试使用自定义过滤器将其设置为空通

namespace App\Conduction\AppBundle\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Ramsey\Uuid\Uuid;

final class UuidFilter extends AbstractContextAwareFilter 
{
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {

        // otherwise filter is applied to order and page as well
        if (
                !$this->isPropertyEnabled($property, $resourceClass) ||
                !$this->isPropertyMapped($property, $resourceClass)
                ) {
                    return;
                }

                $value =pack("h*", str_replace('-', '', $value));

                $parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters

                $queryBuilder
                ->andWhere("$property =:$parameterName")
                ->setParameter($parameterName, $value);
    }

    // This function is only used to hook in documentation generators (supported by Swagger and Hydra)
    public function getDescription(string $resourceClass): array
    {
        if (!$this->properties) {
            return [];
        }

        $description = [];
        foreach ($this->properties as $property => $strategy) {
            $description["uuid"] = [
                    'property' => $property,
                    'type' => 'string',
                    'required' => false,
                    'swagger' => [
                            'description' => 'This filter allows to search the api based on object uuids',
                            'name' => 'UUID',
                            'type' => 'Primary identifier',
                    ],
            ];
        }

        return $description;
    }
}

但是随后我遇到了一个教义错误“ [语义错误]第0行,'platform =:platform_p1'附近的col 76:错误:未定义'platform'。”

当然,这表明平台尚未定义,但据我所知。

---更新:解决了! ---

Interesting thought, but unfortunately that also returns zero results. But interesting stuff is happening. So using the IRI doctrine does get the proper intel -> as seen in the logs as:
[2018-10-30 10:00:50] doctrine.DEBUG: SELECT o0_.slug AS slug_0, o0_.id AS id_1, o0_.name AS name_2, o0_.description AS description_3, o0_.date_created AS date_created_4, o0_.date_published AS date_published_5, o0_.date_modified AS date_modified_6, o0_.date_deleted AS date_deleted_7, o0_.date_checkout AS date_checkout_8, o0_.version AS version_9, o0_.address_id AS address_id_10, o0_.organisation_id AS organisation_id_11, o0_.default_tax_id AS default_tax_id_12, o0_.member_group_id AS member_group_id_13, o0_.admin_group_id AS admin_group_id_14, o0_.image_id AS image_id_15, o0_.platform_id AS platform_id_16, o0_.user_owned AS user_owned_17, o0_.user_created AS user_created_18, o0_.user_updated AS user_updated_19, o0_.user_checkout AS user_checkout_20 FROM organisation__organisation o0_ WHERE o0_.platform_id = ? AND o0_.date_deleted IS NULL AND o0_.date_published < ? ORDER BY o0_.id ASC ["[object] (Ramsey\\Uuid\\Uuid: \"13be1406-d9c3-11e8-9d84-5254007d3b24\")","2018-10-30 10:00:50"] []

因此,这里的学说显然已经知道,它是在搜索Ramsy \ Uuid而不是直接搜索“ 13be1406-d9c3-11e8-9d84-5254007d3b2”。 Okey,如果我们不使用平台IRI进行搜索怎么办? 这样可以很好地响应1个对象,并给出以下理论记录

[2018-10-30 10:09:46] doctrine.DEBUG: SELECT o0_.slug AS slug_0, o0_.id AS id_1, o0_.name AS name_2, o0_.description AS description_3, o0_.date_created AS date_created_4, o0_.date_published AS date_published_5, o0_.date_modified AS date_modified_6, o0_.date_deleted AS date_deleted_7, o0_.date_checkout AS date_checkout_8, o0_.version AS version_9, o0_.address_id AS address_id_10, o0_.organisation_id AS organisation_id_11, o0_.default_tax_id AS default_tax_id_12, o0_.member_group_id AS member_group_id_13, o0_.admin_group_id AS admin_group_id_14, o0_.image_id AS image_id_15, o0_.platform_id AS platform_id_16, o0_.user_owned AS user_owned_17, o0_.user_created AS user_created_18, o0_.user_updated AS user_updated_19, o0_.user_checkout AS user_checkout_20 FROM organisation__organisation o0_ WHERE o0_.date_deleted IS NULL AND o0_.date_published < ? ORDER BY o0_.id ASC ["2018-10-30 10:09:46"] []

因此,区别实际上是存在于Ramsy \ Uuid中的,这里的奇特之处在于,如果我们获得.. / api / platforms / 13be1406-d9c3-11e8-9d84-5254007d3b24,我们将获得该平台对象的良好响应。因此,在学说中解析Ramsy \ Uuid似乎是可行的,嗯...。那么那个id字段呢?如果我们只是掌握数据库并进行简单的联接。我们能够加入组织和ID,因此platform.id和organization__organistation.platform_id字段确实包含相同的值。然后是真正的踢手……。

... / api / organisations?platform.id = 13be1406-d9c3-11e8-9d84-5254007d3b24

起作用!这样就以出乎意料的方式解决了我们的问题。它为我们提供了一个理论日志:

[2018-10-30 10:19:15] doctrine.DEBUG: SELECT t0.editable AS editable_1, t0.memberGroup AS memberGroup_2, t0.id AS id_3, t0.name AS name_4, t0.description AS description_5, t0.date_created AS date_created_6, t0.date_published AS date_published_7, t0.date_modified AS date_modified_8, t0.date_deleted AS date_deleted_9, t0.date_checkout AS date_checkout_10, t0.version AS version_11, t0.organisation_id AS organisation_id_12, t13.slug AS slug_14, t13.id AS id_15, t13.name AS name_16, t13.description AS description_17, t13.date_created AS date_created_18, t13.date_published AS date_published_19, t13.date_modified AS date_modified_20, t13.date_deleted AS date_deleted_21, t13.date_checkout AS date_checkout_22, t13.version AS version_23, t13.address_id AS address_id_24, t13.organisation_id AS organisation_id_25, t13.default_tax_id AS default_tax_id_26, t13.member_group_id AS member_group_id_27, t13.admin_group_id AS admin_group_id_28, t13.image_id AS image_id_29, t13.platform_id AS platform_id_30, t13.user_owned AS user_owned_31, t13.user_created AS user_created_32, t13.user_updated AS user_updated_33, t13.user_checkout AS user_checkout_34, t0.parent_id AS parent_id_35, t0.image_id AS image_id_36, t0.platform_id AS platform_id_37, t0.user_owned AS user_owned_38, t0.user_created AS user_created_39, t0.user_updated AS user_updated_40, t0.user_checkout AS user_checkout_41 FROM organisation__organisation_group t0 LEFT JOIN organisation__organisation t13 ON t0.organisation_id = t13.id INNER JOIN organisation__organisation_group_member ON t0.id = organisation__organisation_group_member.group_id WHERE organisation__organisation_group_member.user_id = ? ["[object] (Ramsey\\Uuid\\Uuid: \"154f07d0-d9c3-11e8-bf04-5254007d3b24\")"] []

等等吗?是的如果我们执行api / organisations?platform = / api / platforms / ba8d9958-d8d5-11e8-a9a5-5254007d3b24。教义没有做内部联接,也无法正确解码UUID,但是当我们做api / organisations?platform.id = ba8d9958-d8d5-11e8-a9a5-5254007d3b24。 API平台强制进行内部联接,然后突然起作用……..

好的,我们的问题现在已经解决了,但是我仍然认为这是API平台(支持Ramsy \ Uuid)或Ramsy \ Uuid可能需要研究的东西

3 个答案:

答案 0 :(得分:0)

我非常确定“正确”的搜索将是:

api/organisations?platform=/api/platforms/ba8d9958-d8d5-11e8-a9a5-5254007d3b24

您需要单独使用iri,而不是id

答案 1 :(得分:0)

您可以创建自定义过滤器:

services.yml

    App\Api\Filter\UlidFilter:
        tags: [ 'api_platform.filter' ]

App\Api\Filter\UlidFilter.php

namespace App\Api\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Uid\Ulid;

final class UlidFilter extends AbstractContextAwareFilter
{
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        // otherwise filter is applied to order and page as well
        if (
            !$this->isPropertyEnabled($property, $resourceClass) ||
            !$this->isPropertyMapped($property, $resourceClass, true)
        ) {
            return;
        }

        // Generate a unique parameter name to avoid collisions with other filters
        $parameterName = $queryNameGenerator->generateParameterName($property);
        $queryBuilder
            ->andWhere(sprintf('o.%s = :%s', $property, $parameterName))
            ->setParameter($parameterName, (new Ulid($value))->toBinary());
    }

    // This function is only used to hook in documentation generators (supported by Swagger and Hydra)
    public function getDescription(string $resourceClass): array
    {
        if (!$this->properties) {
            return [];
        }

        $description = [];
        foreach ($this->properties as $property => $strategy) {
            $description[$property] = [
                'property' => $property,
                'type' => 'string',
                'required' => false,
                'swagger' => [
                    'description' => 'Filter Ulid property.',
                    'name' => 'Ulid Search filter',
                    'type' => '',
                ],
            ];
        }

        return $description;
    }
}

在您的实体中:

use App\Api\Filter\UlidFilter;
...
/**
* @ApiResource()
* @ApiFilter(UlidFilter::class, properties={"Field": "exact"})
**/

答案 2 :(得分:0)

我遇到了同样的问题,并找到了对我有用的方法。

我有两个实体导出和列表。

列表中的一对多 -> 导出。

我想通过列表 ID (UUID) 过滤导出。

private db: IDBPDatabase<MyDB>;

  constructor() { 
    this.connectToDb();
  }

  async connectToDb() {
    this.db = await openDB<MyDB>('my-db', 1, {
      upgrade(db) {
        db.createObjectStore('user-store');
      },
    });
  }

  addUser(username: any, email: any) {
    return this.db.put('user-store', {username, email}, 'userDetail'); <===== here
  }
}

interface MyDB extends DBSchema {
  'user-store': {
    key: any;
    value: any;
  }

我发现如果我在 #[ApiFilter(SearchFilter::class, properties: ['listing' => 'exact'])] 执行查询,它不起作用,但是如果我更改查询参数 api/exports?listing={insert IRI or uuid} 则它起作用了。

相反,如果我把过滤器改成这样——

api/exports?listing.id={insert IRI or uuid}

然后我可以在 #[ApiFilter(SearchFilter::class, properties: ['listing.id' => 'exact'])] 进行查询,它运行良好。

我没有意识到这适用于关系的所有属性,因此如果您只列出 /api/exports?listing={insert IRI or uuid} 作为过滤器,您实际上可以过滤 listing 上的任何属性来自您的查询参数,它将起作用(例如listing.id={} 或listing.type 等)

有趣,因为它开辟了其他可能性,但对于这个特定问题,它解决了 uuid 问题。