OroPlatform Forums

Covering OroPlatform topics, including community updates and company announcements.

Forums Forums OroPlatform OroPlatform – How do I? Questions Extending Core Entity

This topic contains 3 replies, has 2 voices, and was last updated by  tomas.hoch 5 years, 10 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
  • #36624

    tomas.hoch
    Participant

    Hello,

    I needed to add relation between PrivacyStatement (my own entity) and core entity Contact.

    I followed your documentation:
    https://oroinc.com/orocrm/doc/2.6/dev-guide/entities/adding-properties
    but it didn’t work. So I debugged loading metadata a bit and I found little bit different way how to extend core entities. It works fine so far for Contact entity, but I’m not sure, if it can’t break templates, routes and other stuff, so I would like confirm with you, if I can use following approach for other entities and in rest of our project.

    1. As first, created migration, which create relation between PrivacyStatement and Contact:

    2. As second, I created my own entity Contact, which extends original \Oro\Bundle\ContactBundle\Entity\Contact, I didn’t create ExtendContact class in Model folder of my bundle as documention shows. When I did that, then it didn’t work. Oro was creating EX_TvContactBundle_Contact proxy class and remove EX_OroContactBundle_Contact proxy class from inheritance chain. Then migrations was failing due to missing property $picture, which is dynamically created in EX_OroContactBundle_Contact from configuration.

    When doctrine loads metadata, I have following inheritance chain:
    Oro\Bundle\BusinessEntitiesBundle\Entity\BasePerson
    Extend\Entity\EX_OroContactBundle_Contact (so configuration for Contact entity can be generate and applied)
    Oro\Bundle\ContactBundle\Entity\Contact
    Tv\Bundle\ContactBundle\Entity\Contact
    What is exactly what I want.

    3. As next, I needed force ORO app use my exteded Contact entity instead of original one. So I reconfigure parameter %oro_contact.entity.class:% in services config.

    4. and for search indexer I needed copy & paste search.yml file from ContactBundle and create my own with my own entity:

    I reached exactly what I wanted:
    1. I have own class of Contact entity, so I can override auto-generated methods and implement my own behaviour (for example add additional validations and protect entity against setting into incorrect state)
    2. I can write unit tests for code or parts of application, which use Contact entity

    I’m very new in ORO environment, so I don’t know if my solution has any disadvantages or can crash application in other parts of application. It works nice so far. Original (Core) Oro code uses my new entity, I can create / update / display contact, I can write unit test.

    Thanks in advance for your opinion.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #36626

    tomas.hoch
    Participant

    Ok, I found one unbreakable barrier in my approach. I didn’t realize, that entity Oro\Bundle\ContactBundle\Entity\Contact is marked as entity via annotation and doctrine not allow to extend another entity. So when I ran rebuild of DB, migrations failed due to:

    ERROR: Table "orocrm_contact" has more than 1 class. Use "getEntityClassesByTableNames" method instead.

    So I created listener on ClassMetadataEvent to change mapping of Oro\Bundle\ContactBundle\Entity\Contact to MappedSuperClass. But it also didn’t help because Contact has relations OneToMany and relations OneToMany in MappedSuperClass are not allowed in doctrine.

    So I would have to update my listener and do something what Sylius does: https://github.com/Sylius/SyliusResourceBundle/blob/master/EventListener/ORMMappedSuperClassSubscriber.php

    My goal is have extended class to be able to:
    1. Write unit tests
    2. Implement my own logic for field added to Contact via ORO migrations

    Any idea how to reach that?

    I would appreciate any feedback / idea.

    #36627

    Andrey Yatsenko
    Moderator

    Hi Tomas,
    First of all extending entities in Symfony is not allowed by design so “2. As second, I created my own entity Contact, which extends original” you should revert this changes as it’s not possible at all.

    Overriding entity metadata can be done using migrations or entity metadata listener. Better to use migrations.

    Here you can find the documentation how to create relations to extended core entities

    https://github.com/oroinc/platform/blob/master/src/Oro/Bundle/EntityExtendBundle/Resources/doc/relations.md

    #36628

    tomas.hoch
    Participant

    Hi Andrey,

    It’s not good news for me. Using migrations seem to be best. Writing own metadata listener would take too much time.

    Thank you for your feedback.

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

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

Back to top