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

IndexBy in OneToMany Annotation is ignored #710

Closed
karatektus opened this issue Dec 18, 2018 · 2 comments
Closed

IndexBy in OneToMany Annotation is ignored #710

karatektus opened this issue Dec 18, 2018 · 2 comments

Comments

@karatektus
Copy link

karatektus commented Dec 18, 2018

I need some of my relations to be indexed for easier access. Therefore i Use the following property annotation:

/**
   * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent", indexBy="slug")
   */
    private $subcategories;

->getSubcategories()->toArray()

does return an array indexed with the slug. so it does work. but the serializer does respect that.

On the other hand, the serializer does respect it when I use doctrine directly via

$categories = $doctrine
            ->getRepository(Category::class)
            ->createQueryBuilder('c', 'c.slug');

My current solution is to not expose the property directly but use the dumbest virtualproperty ever:

    /**
     * @return Collection
     * 
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("subcategories")
     * @Serializer\Expose()
     */
    public function getSubCategoriesWithKey()
    {
            return $this->getSubcategories();
    }

but that feels pretty silly :x

@goetas
Copy link
Collaborator

goetas commented Dec 18, 2018

I do not see any @Serializer\Type annotation in your snippets...

You might need something as @Serializer\Type("ArrayCollection<string,App\Entity\Category>")

@karatektus
Copy link
Author

well. that fixed it. Thanks!

@goetas goetas closed this as completed Dec 18, 2018
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

2 participants