FriendsOfSymfony / FOSElasticaBundle

Elasticsearch PHP integration for your Symfony project using Elastica.
http://friendsofsymfony.github.io
MIT License
1.24k stars 795 forks source link

Inject a custom Service into a ElasticaToModelTransformer #1889

Closed pl1zahd closed 5 months ago

pl1zahd commented 2 years ago

Hi,

first up, thanks for creating and maintaining such a wonderful piece of software!

I use the Doctrine ORM driver for my project in combination with Version 6.2.0 of this bundle. I have a use-case where it would be useful for me have another Service to be injected into a custom ElasticaToModelTransformer.

When I add no additional Services, I can already use my custom Transformer, but whenever I try to add another Service into the Constructor, it seems that the Index ist not registered in the RepositoryManager.

Every way I tried so far ended either with the Message No repository is configured for index "objects".

I've already tried to extend FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer, FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer and to just implement the FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface.

Here is my configuration:

fos_elastica:
  clients:
    default: { url: 'http://elasticsearch:9200/' }
  indexes:
    objects:
      persistence:
        driver: orm #the driver can be orm, mongodb or phpcr
        model: ACME\Entity\ACMEObject
        model_to_elastica_transformer:
          service: ACME\Transformer\ACMEObjectModelToElasticaTransformer
        elastica_to_model_transformer:
          service: ACME\Transformer\ElasticaToACMEObjectModelTransformer
        persister:
          service: ACME\Persister\ObjectPersister
        listener:
          logger: true

Is it just not possible to add another Service via autowire / autoconfigure or do I have to decorate a service to overwrite the parameters?

If any information is missing that you need to have, let me know and I will update my question accordingly.

Thanks for any help in advance!

SzymonKaminski commented 2 years ago

Not sure if it is the only way, but I've just used setter injection

class CategoryTransformer extends ElasticaToModelTransformer
{
    private IriConverterInterface $iriConverter;

    public function setIriConverter(IriConverterInterface $iriConverter): void
    {
        $this->iriConverter = $iriConverter;
    }

and

    CategoryTransformer:
        calls:
            - setPropertyAccessor:
                  - "@fos_elastica.property_accessor"
            - setIriConverter:
                  - "@api_platform.symfony.iri_converter"