Magento 2 Indexing&Reindex

在本文中,我们将学习如何在Magento 2中创建Indexer Reindex。索引器是Magento 2 Indexing中的一个重要特性。要了解如何创建Hello World模块,可以在此处阅读

我们将使用示例模块Mageplaza_HelloWorld进行此练习。请查看我们之前的帖子,了解如何在Magento 2中创建示例模块。

索引是Magento转换数据(如产品,类别等)的方式,以提高店面的性能。随着数据的变化,转换后的数据必须更新或重新编制索引。Magento有一个非常复杂的架构,可以在许多数据库表中存储大量商家数据(包括目录数据,价格,用户,商店等)。为了优化店面性能,Magento使用索引器将数据累积到特殊表中。

例如,假设您将商品的价格从8.99美元更改为6.99美元。Magento 必须重新索引价格变化以在店面上显示它。

如果没有索引,Magento必须计算每个产品的价格,考虑购物车价格规则,捆绑定价,折扣,等级定价等。加载产品的价格需要很长时间,可能导致购物车放弃。

创建索引器概述

让我们开始创建一个自定义索引器:

  • 第1步:创建Indexer配置文件
  • 第2步:创建Mview配置文件
  • 第3步:创建Indexer类
  • 第4步:运行测试

创建索引器配置文件

此配置文件将定义索引器。

文件 app/code/Mageplaza/HelloWorld/etc/indexer.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="mageplaza_helloworld_indexer" view_id="mageplaza_helloworld_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test">
        <title translate="true">Mageplaza HelloWorld Indexer</title>
        <description translate="true">HelloWorld of custom indexer</description>
    </indexer>
</config>

在此文件中,我们使用以下属性声明一个新的索引器进程:

  • id属性用于标识此索引器。当您要通过命令行检查状态,模式或重新索引此索引器时,可以调用它。
  • view_id是视图元素的id,其将在被定义mview的配置文件。
  • class属性是我们处理索引器方法的类的名称。

简单的Magento 2索引将有一些子元素:

  • title元素用于在索引器网格中显示时定义标题。
  • description元素用于在索引器网格中显示时定义此描述。

创建Mview配置文件

mview.xml文件用于跟踪某个实体的数据库更改并运行更改句柄(execute()方法)。

文件: app/code/Mageplaza/HelloWorld/etc/mview.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
    <view id="mageplaza_helloworld_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test" group="indexer">
        <subscriptions>
            <table name="catalog_product_entity" entity_column="entity_id" />
        </subscriptions>
    </view>
</config>

在这个文件中,我们定义了一个带有id属性的视图元素,用于从索引器调用,以及一个包含该execute()方法的类。订阅中的表更改时,将运行此方法。

为了声明表,我们使用表名和该表的列,该表将被发送到该execute()方法。在这个例子中,我们声明了表catalog_product_entity。因此,只要保存了一个或多个产品,Mageplaza\HelloWorld\Model\Indexer\Test就会调用类中的execute()方法。

创建Indexer类

按照indexer.xmlmview.xml上述情况,我们将定义一个索引类对他们俩的:Mageplaza\HelloWorld\Model\Indexer\Test

文件: app/code/Mageplaza/HelloWorld/Model/Indexer/Test.php

<?php
namespace Mageplaza\HelloWorld\Model\Indexer;

class Test implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{
	/*
	 * Used by mview, allows process indexer in the "Update on schedule" mode
	 */
	public function execute($ids){

		//code here!
	}

	/*
	 * Will take all of the data and reindex
	 * Will run when reindex via command line
	 */
	public function executeFull(){
		//code here!
	}
   
   
	/*
	 * Works with a set of entity changed (may be massaction)
	 */
	public function executeList(array $ids){
		//code here!
	}
   
   
	/*
	 * Works in runtime for a single entity using plugins
	 */
	public function executeRow($id){
		//code here!
	}
}

您可以编写代码以在Indexer类的方法中将数据添加到索引器表。

在此之后,请刷新缓存并System > Index Management从后端转到查看结果。它将显示如下:

按命令运行Reindex

通过命令行运行reindex

php bin/magento indexer:reindex
赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论