OroPlatform Forums

Covering OroPlatform topics, including community updates and company announcements.

Forums Forums OroPlatform OroPlatform – How do I? Questions OneToMany relationship

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

Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.

  • Creator
    Topic
  • #35857

    Ximousse
    Participant

    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
    Replies
  • #35858

    Yevhen Shyshkin
    Participant

    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.

    #35859

    Ximousse
    Participant

    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.

    #35860

    Ximousse
    Participant

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

    #35861

    Ximousse
    Participant

    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.

    #35862

    Yevhen Shyshkin
    Participant

    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.

    #35863

    Ximousse
    Participant

    I had the time to dig a bit into this, and I’m still a bit confused:
    the problem I have comes from this: https://github.com/symfony/symfony/issues/9375
    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.

    #35864

    Yevhen Shyshkin
    Participant

    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)

The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.

Back to top