floriansemm / SolrBundle

Solr-Integration into Symfony and Doctrine2
http://floriansemm.github.io/SolrBundle
MIT License
123 stars 73 forks source link

Allow getter on non objects to query the entity itself #171

Closed dan-haworth closed 6 years ago

dan-haworth commented 6 years ago

Firstly, great work on this awesome bundle, it's saved me HOURS of work! :)

I have a fairly simple requirement, when indexing I'm looking to have the bundle run the getter method on the entity being indexed rather than just query the field value from the class property. The reason for this is I have some fields that aren't part of the ORM mapping but do need to appear in Solr for searching against.

For example: In this particular case, it's a product bar code string which is actually (for reasons I don't want to get into.. legacy code) stored in a serialised array field in the entity. I've created a non doctrine mapped field called barcode with the appropriate setters / getters and modified the getter to pull the barcode out of the serialised data at call time if it has not already done so (so basically, sees the property is null, does the work to extract it from the serialised data and sets the property to what it should be before returning the value).

To put it simply, when I have the following field defined:

/**                                                
 * Virtual field, only used for solr indexing      
 * @Solr\Field(name="barcode", getter="getBarcode")
 */                                                
private $barcode;                                  

During indexing, I would like the indexer to call $entity->getBarcode() for the value, rather than reading the contents of the $barcode property (which after Doctrine hydration will be empty).

I tested a modification in a fork dan-haworth/SolrBundle@07b8bdb31a0c3cc8d0f884f54f6a7f69f38c5106 to test this idea out, however it was just a quick and dirty hack to see if it could be done ;)

I'm unable to use a doctrine post load event as this then causes a multitude of extra work when using the application, I did try hooking the SynchronizationFilter callback in the hope I could run these getters before indexing however SolrBundle appears to read the values first and then run the callback to see if the entity should be indexed which obviously defeats the point :joy:

Any other suggestions also happily accepted as well!

Many thanks,

Dan

dxmaster commented 6 years ago

+1 Virtual field will be very helpfull. In my case virtual field should be calcuated from some fields stored in ORM, but this fields not needed in Solr.

floriansemm commented 6 years ago

@dan-haworth this is a bug. The idea behind getter was to transform complex data structure like objects or in your case serialized data into a "flat" format.