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

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

问题描述

我在我的项目中使用 FOSElasticaBundle 和 Doctrine,我的代码适用于使用 Doctrine 生命周期事件的选择性索引更新.我遇到的问题是我是否单独更新相关实体.

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.

我依靠 JMSSerializer 组来建立映射.我明白这可能不是长期做事的最佳方式.

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.

推荐答案

我想我已经在这个页面上找到了解决方案 https://groups.google.com/forum/#!topic/elastica-php-client/WTONX-zBTI4谢谢卡夏诺

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

基本上,您需要扩展 FOSElasticaBundleDoctrineORMListener,以便您可以查找相关实体,然后也更新它们的索引.

Basically you need to extend the FOSElasticaBundleDoctrineORMListener so you can look for related entities and then update their index as well.

class CompanyListener extends BaseListener
{

    /** @var SymfonyComponentDependencyInjectionContainerInterface */
    private $container;

    public function setContainer(SymfonyComponentDependencyInjectionContainerInterface $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 DoctrineORMEventLifecycleEventArgs $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(DoctrineCommonEventArgs $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: 'xxxRMSBundleEventListenerCompanyListener'
    arguments:
        - '@fos_elastica.object_persister.application.company'
        - 'xxxRMSBundleEntityCompany'
        - ['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天全站免登陆