Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something wrong with relations #516

Open
s-fog opened this issue Nov 11, 2023 · 2 comments
Open

Something wrong with relations #516

s-fog opened this issue Nov 11, 2023 · 2 comments

Comments

@s-fog
Copy link

s-fog commented Nov 11, 2023

PHP: 8.2
Symfony: ^6
Api platform: ^3.2
Admin api platform: ^3.4.4

I created entity ProposalType.php

#[ORM\OneToMany(mappedBy: 'proposalType', targetEntity: ProposalParam::class)]
private Collection $proposalParams;

    public function __construct()
    {
        $this->proposalParams = new ArrayCollection();
    }

    public function getProposalParams(): Collection
    {
        return $this->proposalParams;
    }

    public function addProposalParam(ProposalParam $proposalParam): static
    {
        if (!$this->proposalParams->contains($proposalParam)) {
            $this->proposalParams->add($proposalParam);
            $proposalParam->setProposalType($this);
        }

        return $this;
    }

    public function removeProposalParam(ProposalParam $proposalParam): static
    {
        if ($this->proposalParams->removeElement($proposalParam)) {
            // set the owning side to null (unless already changed)
            if ($proposalParam->getProposalType() === $this) {
                $proposalParam->setProposalType(null);
            }
        }

        return $this;
    }

And another entity ProposalParam.php

    #[ORM\ManyToOne(inversedBy: 'proposalParams')]
    #[ORM\JoinColumn(nullable: false)]
    private ?ProposalType $proposalType = null;
    
    public function getProposalType(): ?ProposalType
    {
        return $this->proposalType;
    }

    public function setProposalType(?ProposalType $proposalType): static
    {
        $this->proposalType = $proposalType;

        return $this;
    }

This is pretty simple ManyToOne relation. Generated by make:entity. I'm dont modified this.
Wanted that ProposalParam entity has ProposalType entity by relation, it is simple.

I can create ProposalType via api platform with POST request to /api/proposal_types. And it is successfull.
Then i can set this ProposalType in ProposalParam via POST request to /api/proposal_types. And it is successfull.
Records are inside database and set up like needed.

When i go to admin panel and send request to create ProposalParam i can set ProposalType.

image

But request sends as array

Без имени

And error occured Nested documents for attribute "proposalType" are not allowed. Use IRIs instead.
And this is expected, we send array, but api wants IRI.

App.js in admin pretty standart, do not modify something

import { HydraAdmin } from "@api-platform/admin";

export default () => (
    <HydraAdmin entrypoint="http://localhost:9191/api" />
);

Main goal is why system generate select where i need to specify array of ProposalTypes, instead of one ProposalType. Can someone help with it?

@clegginabox
Copy link

I'm also having the same issue.

#[ORM\Entity]
class Prize
{
    #[ORM\ManyToOne(targetEntity: Pot::class, inversedBy: 'prizes')]
    private Pot $pot;
}
#[ORM\Entity]
class Pot
{
    #[ORM\OneToMany(targetEntity: Prize::class, mappedBy: 'pot')]
    private Collection $prizes;
}

Using the swagger docs I've manually created some Pots and then created some Prizes linked to the Pots via the IRI

{
  "@context": "/contexts/Pot",
  "@id": "/pots",
  "@type": "hydra:Collection",
  "hydra:totalItems": 2,
  "hydra:member": [
    {
      "@id": "/pots/018c5371-d342-7488-bb5a-cfbe322370c7",
      "@type": "Pot",
      "id": "018c5371-d342-7488-bb5a-cfbe322370c7",
      "name": "Pot 1",
      "prizes": [
        "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe"
      ]
    },
    {
      "@id": "/pots/018c537c-9f52-7d8e-bfdd-9cd5b1bd0095",
      "@type": "Pot",
      "id": "018c537c-9f52-7d8e-bfdd-9cd5b1bd0095",
      "name": "Pot 2",
      "prizes": []
    }
  ]
}
{
  "@context": "/contexts/Prize",
  "@id": "/prizes",
  "@type": "hydra:Collection",
  "hydra:totalItems": 2,
  "hydra:member": [
    {
      "@id": "/prizes/018c476e-6474-7097-8be9-fe9d04ba448b",
      "@type": "Prize",
      "id": "018c476e-6474-7097-8be9-fe9d04ba448b",
      "name": "Test Prize 1"
    },
    {
      "@id": "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe",
      "@type": "Prize",
      "id": "018c5373-d49f-79db-b941-7e63c07ddcfe",
      "name": "Prize 1",
      "pot": "/pots/018c5371-d342-7488-bb5a-cfbe322370c7"
    }
  ]
}

However if I go into the admin and go to edit Prize 1 it's allowing me to select multiple pots.

Screenshot 2023-12-10 at 11 36 23

If I just change the name of the Prize then the PUT request to the API is correct.

{
  "@context": "/contexts/Prize",
  "@id": "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe",
  "@type": "Prize",
  "id": "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe",
  "name": "Prize 13",
  "pot": "/pots/018c5371-d342-7488-bb5a-cfbe322370c7",
  "originId": "018c5373-d49f-79db-b941-7e63c07ddcfe"
}

If I touch the pots select it's like it changes to be a multi select because the PUT request changes to an array of pots

{
  "@context": "/contexts/Prize",
  "@id": "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe",
  "@type": "Prize",
  "id": "/prizes/018c5373-d49f-79db-b941-7e63c07ddcfe",
  "name": "Prize 13",
  "pot": [
    "/pots/018c5371-d342-7488-bb5a-cfbe322370c7"
  ],
  "originId": "018c5373-d49f-79db-b941-7e63c07ddcfe"
}

@krisdante
Copy link

I have the same problem for all ManyToOne relations. There shall be SELECT to pick one relation. There is an interface to assign multiple values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants