Forums OroPlatform OroPlatform – How do I? Questions OneToMany relationship

This topic contains 7 replies, has 2 voices, and was last updated by  Yevhen Shyshkin 3 years, 2 months ago.

  • Creator
  • #30209


    Hello all,

    I’m trying to implement a small app with OroPlatform and I have the following problem:
    I have an entity ‘Person’ which is linked through a OneToMany relationship to an entity ‘SocialNetwork’, here are the annotations:

    I tried to do the same as the Contact entity and Address entity of the OroCRM bundle but whenever I try to post a form to create a new entity Contact without filling the social network fields I have the following error:

    whereas it’s not a problem to leave the address fields empty in OroCRM while creating a new contact.
    In a “classical” bundle I would normally set the default argument of addSocialNetwork to null but it doesn’t work (and it’s not used in OroCRM).
    Am I missing something ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
  • #30210

    Yevhen Shyshkin

    Hello, Ximousse.

    Probably, you should look at Oro\Bundle\FormBundle\Form\EventListener\CollectionTypeSubscriber::postSubmit – it checks collection for empty elements and remove them. Collection form type must be oro_collection or you can add this subscriber manually to your collection. Also to support this functionality your entity must implement interface Oro\Bundle\FormBundle\Entity\EmptyItem.



    Thank you Yevhen for your hint,
    However I do have in my collection type:

    and my Entity extends an abstract class with:

    I’ll dig in the CollectionTypeSubscriber to try to find why it’s nor working for me.



    Ok, nevermind it was a simple cache problem so the EmptyItem works fine (and is extremely useful). Thanks!



    Ok I spoke too fast. I still have the problem:
    In CollectionTypeSubscriber the following condition:

    never gets fired because $item is null if I don’t fill at least one field of the entity in the OneToMany relationship.


    Yevhen Shyshkin

    Then you should check when and why null added to your collection – f.e. you can add breakpoint to add method. Probable reason – incorrect configuration of form type that should return only objects.



    I had the time to dig a bit into this, and I’m still a bit confused:
    the problem I have comes from this:
    in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php

    thus, if I remove the required: false for my “SocialNetworkCollection” from my FormType I don’t have the problem anymore.
    What bothers me is that the field is now marked as “required” (even though I can now leave it empty) and in OroCRM/ContactBundle/Form/Type/ContactType all the collections linked to the contact entity have the required option set to false.


    Yevhen Shyshkin

    Thank you for sharing your results!

    If you still need to make field not required, you can pass custom “empty_data” option as empty instance of handled object.

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.