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

Can I use Array/Collection of object? #137

Closed
ili101 opened this issue May 21, 2024 · 1 comment
Closed

Can I use Array/Collection of object? #137

ili101 opened this issue May 21, 2024 · 1 comment

Comments

@ili101
Copy link

ili101 commented May 21, 2024

I have an Entry, ApiResource. I also created another object and added it as a parameter and it's serialized nicely.

    #[ORM\Column(nullable: true, type: 'json_document')]
    private ?Choice $choices = null;

But I need more than one so I tried to make it an Array or Collection, for example:

{
  "id": 1,
  "name": "me",
  "choices": [
    {
      "label": "admin",
      "value": "admin",
      "default": false
    },
    {
      "label": "user",
      "value": "user",
      "default": false
    }
  ],
}

If I set it as an array it works but it becomes a generic array with sub-arrays and not Choice[]

    /**
     * @var Choice[]|null
     */
    #[ORM\Column(nullable: true, type: 'json_document')]
    private ?array $choices = null;

if I make it an ArrayCollection I get an error "Could not denormalize object of type "Doctrine\Common\Collections\Collection", no supporting normalizer found."

Can this be done using this module? If so, what is the recommended way to configure it?
Thank you

@ili101
Copy link
Author

ili101 commented May 23, 2024

ok managed to get array working. if you don't have:

    public function addChoice(Choice $choice): void
    {
        $this->choices[] = $choice;
    }

the subobjects become arrays, if you add it you get objects.

@ili101 ili101 closed this as completed Jun 19, 2024
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

1 participant