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
608 stars 63 forks source link

Fix invalid offset access on relationship without `inversedBy` and `mappedBy` #561

Closed alexandre-daubois closed 4 months ago

alexandre-daubois commented 4 months ago

When dealing with unilateral one-to-one relation like this:

class User
{
    #[OneToOne]
    public Address $adress
}

We have this error popping:

In AssociationMapping.php line 243:

   [OutOfRangeException]
   Unknown property "mappedBy" on class Doctrine\ORM\Mapping\OneToOneOwningSideMapping

 Exception trace:
   at /private/var/www/backoffice-api/vendor/doctrine/orm/src/Mapping/AssociationMapping.php:243
  Doctrine\ORM\Mapping\AssociationMapping->offsetGet() at
 /private/var/www/backoffice-api/vendor/zenstruck/foundry/src/Factory.php:463
  Zenstruck\Foundry\Factory::getRelationshipMetadata() at
 /private/var/www/backoffice-api/vendor/zenstruck/foundry/src/Factory.php:411
  Zenstruck\Foundry\Factory->normalizeAttribute() at
 /private/var/www/backoffice-api/vendor/zenstruck/foundry/src/Factory.php:111
  Zenstruck\Foundry\Factory->create() at
 /private/var/www/backoffice-api/src/DataFixtures/Story/User/UserStory.php:29
  App\DataFixtures\Story\Kitchen\KitchenBlockStory->build() at
 /private/var/www/backoffice-api/vendor/zenstruck/foundry/src/StoryManager.php:52

It happened when trying to upgrade to DBAL4/ORM3. I think this fix can help support latest Doctrine's versions 🙂

nikophil commented 4 months ago

Hi @alexandre-daubois

thanks for this!

the failures you have here are unrelated to your fix. I'm gonna fix this very soon.

We need to work on ORM 3/DBAL 4 compatiblity with Foundry, I'd be curious to have you feedback about it.

when looking at this PR and the related failures in the CI, I'd say there should not be too much work (and we can see that the CI hit your error)

alexandre-daubois commented 4 months ago

Alright, thank you for having a look! Feel free to ping me to try the "real" fix for this problem, I'd be happy to help and give feedback 😊