core icon indicating copy to clipboard operation
core copied to clipboard

Setting uriVariables without a direct relationship between classes

Open lermontex opened this issue 3 years ago • 2 comments

Description
Is it possible to set up a variable (uriVariables) if there is no direct relationship between classes (2.7@dev)? Unfortunately, I did not find an example in the documentation. Is such a possibility planned?

Example

#[ORM\Entity(repositoryClass: UserRepository::class)]
class User
{
    #[ORM\Id]
    #[ApiProperty(identifier: true)]
    private Ulid $id;

    #[ORM\OneToMany(mappedBy: "user", targetEntity: Domain::class, orphanRemoval: true)]
    private iterable $domains;
}
#[ORM\Entity(repositoryClass: DomainRepository::class)]
#[ApiResource(
    uriVariables: [
        'userId' => new Link(fromProperty: 'domains', fromClass: User::class),
        'id' => new Link(fromClass: self::class, identifiers: ['id'])
    ],
)]
#[Get(
    uriTemplate: '/users/{userId}/domains/{id}.{_format}',
)]
class Domain
{
    #[ORM\Id]
    #[ApiProperty(identifier: true)]
    private Ulid $id;

    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: "domains")]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $user = null;

    #[ORM\OneToMany(mappedBy: "domain", targetEntity: ResetTokenRequest::class, orphanRemoval: true)]
    private iterable $resetTokenRequests;
}
#[ORM\Entity(repositoryClass: ResetTokenRequestRepository::class)]
#[ApiResource(
    uriVariables: [
        'userId' => new Link(...), // <<<---------???
        'domainId' => new Link(fromProperty: 'resetTokenRequests', fromClass: Domain::class),
        'id' => new Link(fromClass: self::class, identifiers: ['id'])
    ],
    normalizationContext: ['groups' => ['user:domain:reset_token_request:read_normalization']],
)]
#[Get(uriTemplate: '/users/{userId}/domains/{domainId}/reset_token_requests/{id}.{_format}',)]
class ResetTokenRequest
{
    #[ORM\Id]
    #[ApiProperty(identifier: true)]
    private Ulid $id;

    #[ORM\ManyToOne(targetEntity: Domain::class)]
    #[ORM\JoinColumn(nullable: false)]
    private Domain $domain;
}

Is it possible to set userId variable with Link?

lermontex avatar Apr 20 '22 17:04 lermontex

Yes this should work, juste define your userId link between Domain and User I'm guessing new Link(fromProperty: 'domains', fromClass: User::class)

soyuka avatar Apr 22 '22 12:04 soyuka

Unfortunately, it didn't work

curl -X 'GET' \
  'https://localhost/api/users/01G1CJ7WF6B46AWM4EWTKZ1YM1/domains/01G1CJ7WF6B46AWM4EWTKZ1YM2/reset_token_requests?page=1' \
  -H 'accept: application/ld+json'

[Semantical Error] line 0, col 94 near 'user IN (SELECT': Error: Class App\\Entity\\ResetTokenRequest has no field or association named user

query getResetTokenRequests{
  resetTokenRequests{
    edges{
      node{
        id
      }
    }
  }
}

"Unable to generate an IRI for the item of type "App\Entity\ResetTokenRequest""

Read more here: https://github.com/lermontex/api-platform-bug-example

lermontex avatar Apr 24 '22 01:04 lermontex

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 04 '22 21:11 stale[bot]

@lermontex did you resolve this?

mwierzbi avatar Nov 10 '22 09:11 mwierzbi

@mwierzbi no, unfortunately, due to the lack of documentation I had to abandon the idea of using my own uriVariables

lermontex avatar Nov 11 '22 21:11 lermontex

Running into this myself. We have a class that has a relationship through an intermediate class, and want the ID in the url.

I've attempted to fix it but haven't figured it out yet. If I do I will report back.

nesl247 avatar Dec 16 '22 05:12 nesl247

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 14 '23 13:02 stale[bot]

Hi, I encounter the same problem. I want to use ID in the uri but the classes only have a relationship through an intermediate class. Does anybody have a solution for this issue?

ChrisCornelissen avatar Aug 18 '23 06:08 ChrisCornelissen

please create a test case or a reproducer without knowing what you're doing exactly it's impossible to answer

soyuka avatar Aug 18 '23 11:08 soyuka

same issue +1

(api-platform/core 3.1)

johnkhansrc avatar Aug 30 '23 09:08 johnkhansrc

This bug is targeting 2.7, as API Platform 3.2 is out, version 2.7 has reached end of life. Therefore we'll close this issue.

We recommend to upgrade to API Platform 3.0, Les-Tilleuls.coop can offer paid support to help or even migrate your projects if they have tests.

We want to fund a Long Term Stable version of API Platform, if you or your organization would like to contribute to LTS support, please visit our Open Collective crowdfunding.

soyuka avatar Oct 17 '23 09:10 soyuka