This topic contains 3 replies, has 2 voices, and was last updated by Frank 4 years, 8 months ago.
- Topic
Hi there,
I am trying to extend core OroShoppingListRepository according to:
https://stackoverflow.com/questions/35942910/how-to-override-bundled-doctrine-repository-in-symfonyBut, 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:
1234567891011121314151617181920212223242526272829303132333435363738namespace MyShoppingListBundle\EventListener;use Doctrine\Common\EventSubscriber;use Doctrine\ORM\Event\LoadClassMetadataEventArgs;use Doctrine\ORM\Events;class LoadClassMetadataSubscriber implements EventSubscriber{/*** @inheritdoc*/public function getSubscribedEvents(){return [Events::loadClassMetadata];}/*** @param LoadClassMetadataEventArgs $eventArgs*/public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs){/*** @var \Doctrine\ORM\Mapping\ClassMetadata $classMetadata*/$classMetadata = $eventArgs->getClassMetadata();// entityif ($classMetadata->getName() !== 'Oro\Bundle\ShoppingListBundle\Entity\ShoppingList') {return;}// repository$myClassName = 'MyShoppingListBundle\Entity\Repository\MyShoppingListRepository';$classMetadata->setCustomRepositoryClass($myClassName);}2. Registered event-subscriber according to:
https://symfony.com/doc/3.4/doctrine/event_listeners_subscribers.html123456789101112131415161718192021222324252627<?phpnamespace MyShoppingListBundle\DependencyInjection;use Symfony\Component\Config\FileLocator;use Symfony\Component\DependencyInjection\ContainerBuilder;use Symfony\Component\DependencyInjection\Loader;use Symfony\Component\HttpKernel\DependencyInjection\Extension;class MyShoppingListExtension extends Extension{/*** {@inheritDoc}** @throws \Exception*/public function load(array $configs, ContainerBuilder $container){$loader = new Loader\YamlFileLoader($container,new FileLocator(__DIR__ . '/../Resources/config'));$loader->load('services.yml');}}1234567# MyShoppingListBundle/Resources/config/services.ymlservices:MyShoppingListBundle\EventListener\LoadClassMetadataSubscriber:tags:- { name: doctrine.event_subscriber, event: loadClassMetadata, connection: default }3. Creating my custom repository, with test-method ‘findLabel()’
12345678910111213141516<?phpnamespace MyShoppingListBundle\Entity\Repository;use Oro\Bundle\ShoppingListBundle\Entity\Repository\ShoppingListRepository;class MyShoppingListRepository extends ShoppingListRepository{public function findByLabel($str = null) {return $str;}}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:
1234567891011121314151617181920212223242526272829303132<?phpnamespace MyShoppingListBundle\Manager;use Oro\Bundle\ShoppingListBundle\Manager\CurrentShoppingListManager;use Oro\Bundle\ShoppingListBundle\Entity\ShoppingList;class MyCurrentShoppingListManager extends CurrentShoppingListManager{public function test() {$str = 'Hello');$label = $this->getShoppingListRepository()->findByLabel($label));dump($label);}*** @return ShoppingList*/private function getShoppingListRepository(){return $this->doctrine->getManagerForClass(ShoppingList::class)->getRepository(ShoppingList::class);}}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
The forum ‘OroPlatform’ is closed to new topics and replies.