zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
609 stars 63 forks source link

fix: create ArrayCollection if needed #645

Closed nikophil closed 5 days ago

nikophil commented 1 week ago

please review https://github.com/zenstruck/foundry/pull/653 before, as this PR is built is the top of the other

fixes https://github.com/zenstruck/foundry/issues/474

this is a really naive approach, nonetheless, this could be helpful. It will only work if the property is actually type hinted with Doctrine\Common\Collections\Collection. It won't create an ArrayCollection if the property is only type hinted with phpdoc, or if it has a complex type hint (for instance, I saw in few occasions something like private array|Collection $relations).

Another cleaner approach would have been to use doctrine metadata in Hydrator but this seemed convoluted.

But I think the current implementation is good enough for now, since the type hint with Collection is how it is documented by Doctrine.

Maybe in the future we might support other \Traversable objects?