symfony - Change ES data on nested object changes automatically -
types: product: mappings: title: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string } status: brand.name: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string } brand: type: "nested" properties: status: ~ persistence: driver: orm model: mybundle\entity\product\product provider: query_builder_method: customproductquerybuilderelastica listener: ~ finder: ~
this mappings type product. customproductquerybuilderelastica contains code populates products have active status , have related brand status active. working if change products admin.
what want when change brand status inactive, related products should removed es.
for have used brand nested of product , created listener explained here , able change brand status every products in es automatically want remove such products when brand status sets inactive. how can achieved in better way?.
after many tries. achieved want. posting code here , try others.
thanks @maercky. have taken reference answer given here
here config.yml file.
types: product: mappings: title: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string } status: brand.name: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string } brand: type: "nested" properties: status: ~ persistence: driver: orm model: xxx\mybundle\entity\product\product provider: query_builder_method: customproductquerybuilderelastica listener: ~ finder: ~
this code go service.yml
fos_elastica.listener.brand.product: class: 'xxx\mybundle\listener\elasticabrandlistener' arguments: - @fos_elastica.object_persister.search.product - ['postpersist', 'postupdate', 'postremove', 'preremove'] - @fos_elastica.indexable calls: - [ setcontainer, [ '@service_container', @fos_elastica.object_persister.search.product ] ] tags: - { name: 'doctrine.event_subscriber' }
and listener brand
<?php namespace xxx\mybundle\listener; use fos\elasticabundle\doctrine\listener baselistener; use doctrine\common\eventargs; use symfony\component\dependencyinjection\containerinterface; use xxx\mybundle\entity\supplier\brand; use fos\elasticabundle\persister\objectpersister; class elasticabrandlistener extends baselistener { /** @var \symfony\component\dependencyinjection\containerinterface */ private $container; private $objectpersisterproducts; public function setcontainer(containerinterface $container,objectpersister $objectpersisterproduct) { $this->container = $container; $this->objectpersisterproducts = $objectpersisterproduct; } /** * @param doctrine\common\eventargs $eventargs */ public function postupdate(eventargs $eventargs) { /** @var $brand brand */ $brand = $eventargs->getentity(); if ($brand instanceof brand) { $this->scheduledforupdate[] = $brand; foreach ($brand->getproducts() $product) { $brand_status = $brand->getstatus(); $product_status = $product->getstatus(); if($brand_status == 'active' && $product_status == 'active'){ $this->objectpersisterproducts->replaceone($product); }else{ $this->objectpersisterproducts->deleteone($product); } } } } } ?>
all works me , contributing others.
Comments
Post a Comment