FOSElasticaBundle icon indicating copy to clipboard operation
FOSElasticaBundle copied to clipboard

Can not use custom search repository

Open ghost opened this issue 4 years ago • 1 comments

I am trying to use a custom repository as described here: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/7f3d131a8cf58cf83f40d85bcd49c03bb0e7caea/doc/cookbook/custom-repositories.md

Here is my code

# config/packages/fos_elastica.yaml

fos_elastica:
    clients:
        default: { url: '%env(ELASTICSEARCH_URL)%' }
    indexes:
        example_index:
            properties:
                myProperty:
                    type: keyword
            persistence:
                model: App\Entity\ExampleEntity
                repository: App\SearchRepository\ExampleRepository
                listener: false
// src/SearchRepository/ExampleRepository.php

namespace App\SearchRepository;

use FOS\ElasticaBundle\Repository;

final class ExampleRepository extends Repository
{
}
// src/Entity/ExampleEntity

namespace App\Entity;

final class ExampleEntity
{
    public string $myProperty;
}
// src/Controller/TestController.php

namespace App\Controller;

use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class TestController extends AbstractController
{
    #[Route('/test', name: 'test')]
    public function test(RepositoryManagerInterface $repositoryManager)
    {
        $repository = $repositoryManager->getRepository('example_index');

        $result = $repository->find('*');

        dd($result);
    }
}

When I go to the "/test" route I get the following Exception:

Error:
Call to a member function getRepository() on null

  at vendor/friendsofsymfony/elastica-bundle/src/Doctrine/ORM/ElasticaToModelTransformer.php:63
  at FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer->getEntityQueryBuilder()
     (vendor/friendsofsymfony/elastica-bundle/src/Doctrine/ORM/ElasticaToModelTransformer.php:41)
  at FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer->findByIdentifiers()
     (vendor/friendsofsymfony/elastica-bundle/src/Doctrine/AbstractElasticaToModelTransformer.php:89)
  at FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer->transform()
     (vendor/friendsofsymfony/elastica-bundle/src/Finder/TransformedFinder.php:51)
  at FOS\ElasticaBundle\Finder\TransformedFinder->find()
     (vendor/friendsofsymfony/elastica-bundle/src/Repository.php:39)
  at FOS\ElasticaBundle\Repository->find()
     (src/Controller/TestController.php:20)
  at App\Controller\TestController->test()
     (vendor/symfony/http-kernel/HttpKernel.php:157)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:79)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:195)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (public/index.php:20)

Am I doing something wrong or is there a bug?

ghost avatar Mar 24 '21 12:03 ghost

The problem could be that I am using the bundle without a database table for my Elasticsearch records. When I add ORM annotations to my entity I get the following error:

Base table or view not found: 1146 Table 'my_database.example_entity' doesn't exist

It seems like the bundle is trying to get the results from a database. Is there a way to use the bundle without storing entries in a database and only storing them in Elasticsearch?

joergmoldenhauer avatar Mar 24 '21 13:03 joergmoldenhauer