OroPlatform Forums

Covering OroPlatform topics, including community updates and company announcements.

Forums OroPlatform Extending core ShoppingListRepository: Mapping problem

This topic contains 3 replies, has 2 voices, and was last updated by  Frank 3 weeks, 4 days ago.

  • Creator
    Topic
  • #40053

    Frank
    Participant

    Hi there,

    I am trying to extend core OroShoppingListRepository according to:
    https://stackoverflow.com/questions/35942910/how-to-override-bundled-doctrine-repository-in-symfony

    But, this results in the error-message:

    Repository for class Oro\Bundle\ShoppingListBundle\Entity\ShoppingList must be instance of MyShoppingListBundle\Entity\Repository\MyShoppingListRepository

    Step by step, I have done the following:

    1. Created an event-subscriber for loadClassMetadata-event:

    2. Registered event-subscriber according to:
    https://symfony.com/doc/3.4/doctrine/event_listeners_subscribers.html

    3. Creating my custom repository, with test-method ‘findLabel()’

    4. Build some custom Manager (which extends OroShoppingListBundle/Manager/CurrentShoppingList) – it is working fine, until I am trying to call a method from my custom repository:

    Calling some Frontend page generates the error above.

    What I am doing wrong?

    It is a mapping issue, obviously.


    A second question would be about the freedom of generation methods in MyShoppingListRepository:
    If I want to create a method like ‘findByVistor()’ calling it in MyCurrentShoppingListManager gernerates an error:

    ‘Visitor is not a field of ShoppingList entity – you can’t use it. Use findByOne, findByUser … instead’

    I would like to extend ShoppingList-entity from OroBundle in my custom bundle. However, it entity-extension is not allowed (without change db-schema/performing migration).

    How I can built a method like findByVisitor() in my custom ShoppingList-repository?

    Thanks a lot in advance
    Frank

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

    Frank
    Participant

    Some supplement/update:

    I found a typo in code (4) above, however it is not changing the mentioned error message above:

    Repository for class Oro\Bundle\ShoppingListBundle\Entity\ShoppingList must be instance of MyShoppingListBundle\Entity\Repository\MyShoppingListRepository

    In phpStorm, $this->getShoppingListRepository()->findByLabel() in row #17 is highlighted, saying me, that

    Method ‘findLabel’ is not found in ‘Oro\Bundle\ShoppingListBundle\Entity\Repository\ShoppingListRepository

    This is consistant with the suspicion that the wrong (non-extended) repository-instance is called in
    MyCurrentShoppingListManager::getShoppingListRepository().

    However, I cannot change ShoppingList::class in the mentioned method (which is OroShoppingListBundle\Entity\ShoppingList) to some ‘extended/customized’ ShoppingList-entity … because this is forbidden!

    How to get out?

    Thanks
    Frank

    #40061
    Andrey Yatsenko
    Andrey Yatsenko
    Moderator

    Hi Frank,

    First of all, you should note that Oro applications are build on top of symfony framework, but not all the things work the same way and you can’t apply all the practices as is.

    If you want to extend an entity from core, take look at this article:
    https://oroinc.com/orocrm/doc/current/dev-guide/entities/adding-properties.

    As for extending the repository, basically, it is not recommended. Instead, I advise to create a new additional repository class, that is not related to the original one and store all the custom methods in it. There is no need to make your repository to extend the EntityRepository, and you can use the original repository by injecting the @doctrine service in your custom repository service.

    The main idea is that the only responsibility of the repository is to store reusable methods that interact with the Doctrine.
    It would be easier to manage and more explicit solution than hacking the entity metadata, that probably will not work as expected with the Oro customizations of Doctrine.

    #40071

    Frank
    Participant

    Hi Andrey,

    thanks for your fast reply!

    1)
    >> if you want to extend an entity from core, take look at this article:
    >> https://oroinc.com/orocrm/doc/current/dev-guide/entities/adding-properties.

    Yes, I know that article. It assumes, that someone wants to add some custom fields to a core entity. This works fine. However a ‘classic’ php-class-extension is not possible in oro application. That’s not a problem for me at all …

    2)
    >> As for extending the repository, basically, it is not recommended.

    Ok. Sounds comprehensible, completely. I will follow your recommandations. Still learning ‘oro’, in small steps ….

    Have a nice day.

    Regards from Germany
    Frank

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

You must be logged in to reply to this topic.

You will be redirected to [title]. Would you like to continue?

Yes No