How to extend / override the base transformer from Elastic result to Doctrine entity in FosElasticaBundle?
I'd like to change the transform
method in FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer
so that I can add a custom property to the retrieved entities.
I tested by hacking that method, and it works exactly as I want.
What's the best solution to override / extend that method without hacking it?
You can override the transformer and define your own by creating a new service. The full story can be read at http://obtao.com/blog/2014/05/advanced-indexing-with-elasticsearch-foselasticabundle/
Shortly: first you define a service like this (in my case I only index one specific entity):
myapp.elastica_to_model.transformer.company:
class: AppBundle\Transformers\ElasticaToCompanyTransformer
arguments: [ @doctrine, "AppBundle\Entity\Company" ]
calls:
- [ setPropertyAccessor, ["@fos_elastica.property_accessor"] ]
tags:
- { name: fos_elastica.elastica_to_model_transformer.app.company }
Then you create the actual service class, where you extend the original transformer and override the transform
method.
Finally you tell FosElastica to use this transformer instead of the original one:
fos_elastica:
indexes:
app:
types:
company:
mappings:
...
persistence:
...
elastica_to_model_transformer:
service: myapp.elastica_to_model.transformer.company