1

I have a question: I need to get communication with algolia, but i cannot get that work, since of leak of documentation. I have symfony 5.4 but algora docs provide only for version 4, second when I trying to get some of my entities indexed i get stuck at the point when i have to provide entityManager which suppose to implement ObjectManager, but that one it is not wired in the service. And how many people suggest we should not use this directly and use EntityManagerInteface instead. So how i have to index my Entity?

What i did:

composer require algolia/search-bundle

Added credentials:

ALGOLIA_APP_ID="U1RFIHY01W"
ALGOLIA_API_KEY="YourAPIKey"

Trying to index in the controller for testing:

public function searchPosts(Post $post, ManagerRegistry $em): JsonResponse
    {
        $posts = $this->postRepository->searchPosts('82819');
        $postsArray = [];

        /**@var Post $post */
        foreach ($posts as $post){
            $postsArray = $post->normalize();
        }

        $this->searchService->index($em->getManager(), $postsArray);


        return new JsonResponse($this->searchService->getConfiguration());
    }

Right now since i use ManagerRegestry i get an error:

get_class(): Argument #1 ($object) must be of type object, int given

Can you help me work around? Thank you

Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16
  • 1) you can inject EntityManagerInterface directly, no need to inject ManagerRegistry. 2) you sure searchPosts supports '82819' as argument and not a Post object? 3) where is $lpostsArray coming from? – LBA Jul 19 '22 at 07:30
  • @LBA i switched to work with `Elasticsearch` because unavailability to reformat their plugin to work with Symfony 5.4 – Serghei Leonenco Jul 24 '22 at 00:12

1 Answers1

0

Separate the two instructions with :

$entityManager= $em->getManager();

and then

$this->searchService->index($entityManager, $lpostsArray);

vinceAmstoutz
  • 676
  • 1
  • 4
  • 18