所有实体更新的所有实体的Symfony2 FOSElasticaBundle更新索引 [英] Symfony2 FOSElasticaBundle update index for all entities related to the entity updated

查看:172
本文介绍了所有实体更新的所有实体的Symfony2 FOSElasticaBundle更新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用FOSElasticaBundle和Doctrine,我的代码适用于使用Doctrine生命周期事件的选择性索引更新。我提出的问题是,如果我单独更新一个相关实体。



例如,一个人可能通过manytomany关系与一家公司相关。如果我直接通过公司实体更新公司名称,那么与该公司有关的人员的索引将会过时,并且与公司的旧名称有关。



I对于如何处理这个有点迷失,有没有人有任何建议?我是否必须依赖于计划的索引更新,同时处理不准确的索引数据,或者有一种方法可以调用与已更新的实体相关的实体的更新。



我依靠JMSSerializer组来建立映射。我感谢这可能不是长期做最好的方法。

解决方案

我想我已经找到了解决方案此页面 https://groups.google.com/forum /#!topic / elastica-php-client / WTONX-zBTI4
感谢Cassiano



基本上你需要扩展FOS \ElasticaBundle \\ \\Doctrine\ORM\Listener,因此您可以查找相关实体,然后更新其索引。

  class CompanyListener extends BaseListener 
{

/ ** @var \Symfony\Component\DependencyInjection\ContainerInterface * /
private $ container;

public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $ container){
$ this-> container = $ container;
}

protected function initialiseJob(){
$ this-> objectPersisterJob = $ this-> container-> get('fos_elastica.object_persister.application.job' );
$ this-> em = $ this-> container-> get('doctrine') - > getEntityManager(); //或者将其移动到postUpdate函数,以便它可以用于所有
}

/ **
* @param \Doctrine\ORM\Event\LifecycleEventArgs $ eventArgs
* /
public function postUpdate(LifecycleEventArgs $ eventArgs)
{
/ ** @var $实体故事* /
$ entity = $ eventArgs-> ; getEntity();

if($ entity instanceof $ this-> objectClass){
if($ this-> isObjectIndexable($ entity)){
$ this-> objectPersister- > replaceOne($实体);
$ this-> initialiseJob();
foreach($ entity-> getJobOpenings()as $ job){
$ this-> objectPersisterJob-> replaceOne($ job);
}
} else {
$ this-> scheduleForRemoval($ entity,$ eventArgs-> getEntityManager());
$ this-> removeIfScheduled($ entity);
}
}
}

public function preRemove(\Doctrine\Common\EventArgs $ eventArgs)
{
$ entity = $ eventArgs-> getEntity();

if($ entity instanceof $ this-> objectClass){

$ this-> scheduleForDeletion($ entity);
$ this-> initialiseJob();
foreach($ entity-> getJobOpenings()as $ job){
$ this-> objectPersisterJob-> replaceOne($ job);
}
}
}


}

,您的服务定义如下

  fos_elastica.listener.application.company:
class :'xxx\RMSBundle\EventListener\CompanyListener'
参数:
- '@ fos_elastica.object_persister.application.company'
- 'xxx\RMSBundle\Entity\Company '
- ['postPersist','postUpdate','postRemove','preRemove']
- id
调用:
- [setContainer,['@service_container']]
标签:
- {name:'doctrine.event_subscriber'}

然后将更新两者的索引: - )


I'm using FOSElasticaBundle and Doctrine in my project, and my code works for the selective index update using the Doctrine lifecycle events. The issue I come up against is if I an update a related entity separately.

For example a person may be related to a company through a manytomany relationship. If I update the company name through company entity directly, then indexes for the person related to the company will be out of date and still relate to the company's old name.

I'm a bit lost as to how to handle this, does anyone have any suggestions? Do I have to rely on a scheduled index update and cope with inaccurate index data in the mean time, or is there a way I can call an update for entities related to the entity that has been updated.

I am relying on JMSSerializer groups to establish the mappings. I appreciate this might not be the best way to do things in the longterm.

解决方案

I think I've found the solution on this page https://groups.google.com/forum/#!topic/elastica-php-client/WTONX-zBTI4 Thanks Cassiano

Basically you need to extend the FOS\ElasticaBundle\Doctrine\ORM\Listener so you can look for related entities and then update their index as well.

class CompanyListener extends BaseListener
{

    /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
    private $container;

    public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container) {
        $this->container = $container;
    }

    protected function initialiseJob() {
        $this->objectPersisterJob = $this->container->get('fos_elastica.object_persister.application.job');
        $this->em = $this->container->get('doctrine')->getEntityManager(); //maybe move this to postUpdate function so it can be used for all
    }

    /**
     * @param \Doctrine\ORM\Event\LifecycleEventArgs $eventArgs
     */
    public function postUpdate(LifecycleEventArgs $eventArgs)
    {
        /** @var $entity Story */
        $entity = $eventArgs->getEntity();

        if ($entity instanceof $this->objectClass) {
            if ($this->isObjectIndexable($entity)) {
                $this->objectPersister->replaceOne($entity);
                $this->initialiseJob();
                foreach ($entity->getJobOpenings() as $job) {
                    $this->objectPersisterJob->replaceOne($job);
                }
            } else {
                $this->scheduleForRemoval($entity, $eventArgs->getEntityManager());
                $this->removeIfScheduled($entity);
            }
        }
    }

    public function preRemove(\Doctrine\Common\EventArgs $eventArgs)
    {
        $entity = $eventArgs->getEntity();

        if ($entity instanceof $this->objectClass) {

            $this->scheduleForDeletion($entity);
            $this->initialiseJob();
            foreach ($entity->getJobOpenings() as $job) {
                $this->objectPersisterJob->replaceOne($job);
            }
        }
    }


}

and your services defined as below

fos_elastica.listener.application.company:
    class: 'xxx\RMSBundle\EventListener\CompanyListener'
    arguments:
        - '@fos_elastica.object_persister.application.company'
        - 'xxx\RMSBundle\Entity\Company'
        - ['postPersist', 'postUpdate', 'postRemove', 'preRemove']
        - id
    calls:
        - [ setContainer, [ '@service_container' ] ]
    tags:
        - { name: 'doctrine.event_subscriber' }

this will then update indexes for both :-)

这篇关于所有实体更新的所有实体的Symfony2 FOSElasticaBundle更新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆