Replies: 1 comment 1 reply
|
The service 'name' (or is it 'id'?) should be the FQCN, not some custom name: Acme\MegaBundle\Repository\FilterTypeRepository:
# class is not needed when service 'name' === FQCN
arguments: ['@doctrine']
tags: ['doctrine.repository_service']Auto wiring makes this a lot easier though. Check out /**
* {@inheritdoc}
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryServiceId = $metadata->customRepositoryClassName;
$customRepositoryName = $metadata->customRepositoryClassName;
if ($customRepositoryName !== null) {
// fetch from the container
if ($this->container && $this->container->has($customRepositoryName)) {
$repository = $this->container->get($customRepositoryName);
if (! $repository instanceof ObjectRepository) {
throw new RuntimeException(sprintf('The service "%s" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).', $repositoryServiceId));
}
return $repository;
}
// if not in the container but the class/id implements the interface, throw an error
if (is_a($customRepositoryName, ServiceEntityRepositoryInterface::class, true)) {
// can be removed when DoctrineBundle requires Symfony 3.3
if ($this->container === null) {
throw new RuntimeException(sprintf('Support for loading entities from the service container only works for Symfony 3.3 or higher. Upgrade your version of Symfony or make sure your "%s" class does not implement "%s"', $customRepositoryName, ServiceEntityRepositoryInterface::class));
}
throw new RuntimeException(sprintf('The "%s" entity repository implements "%s", but its service could not be found. Make sure the service exists and is tagged with "%s".', $customRepositoryName, ServiceEntityRepositoryInterface::class, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
}
if (! class_exists($customRepositoryName)) {
throw new RuntimeException(sprintf('The "%s" entity has a repositoryClass set to "%s", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "%s".', $metadata->name, $customRepositoryName, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
}
// allow the repository to be created below
}
return $this->getOrCreateRepository($entityManager, $metadata);
} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
As written in documentation we must not use FQCN in our reusable bundles service configuration.
So in my bundle
services.yamli have this:While i use this service as argument to other services or controllers explicitly e.g
everything work as expected. But if i try to do
$em->getRepository(FilterType::class)or use entity type hint in controllerill receive following error
Adding this to service.yaml did not solve the problem
Acme\MegaBundle\Repository\FilterTypeRepository: '@acme.repository.filter_type'but if i use FQCN as service name everything works nice.
If i take a look in ContainerRepositoryFactory:47 (DoctrineBundle) with xdebug i see that doctrine knows only about "acme.repository.filter_type" repository and wont use it.
SF: 5.4
All reactions