Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

MAGENTO 2:如何从自定义表中删除或存档记录?

当我们将Magento 2开发到JDA集成时,我们最近需要归档或删除API日志。我们将每个API调用都记录到Magento 2中的自定义日志表中,并且在2周内,我们有超过10万条记录。如果没有归档策略,那么该表肯定会在3个月内成千上万条记录。我们的开发团队创建了一个cron作业,以删除超过一定天数的记录,这些记录可以从admin中轻松配置。

我们在Google上找不到任何类似的内容,因此我们认为为Magento 2用户创建另一个简短而甜蜜的帖子会很好。

这是主要类,可帮助您从自定义表中删除或存档早于x天数的记录

/**
 * Cron job to run jda logs archive
 *
 * @category   Scommerce
 * @package    Scommerce_JdaConnector
 * @author     Scommerce Core Team
 *
 */
 
namespace Scommerce\JdaConnector\Cron;
 
 
class LogsArchive
{
    /**
     * @var \Scommerce\JdaConnector\Helper\Data
     */
    protected $_helper;
 
    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $_resourceConnecton;
 
    /**
     * @param \Scommerce\JdaConnector\Helper\Data $helper
     * @param \Magento\Framework\App\ResourceConnection $resourceConnection
     */
    public function __construct(
        \Scommerce\JdaConnector\Helper\Data $helper,
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->_helper = $helper;
        $this->_resourceConnecton = $resourceConnection;
    }
 
    public function execute()
    {
        if ($this->_helper->getLogsArchiveEnabled()){
            $connection = $this->_resourceConnecton->getConnection();
            $jdaLogTable = $this->_resourceConnecton->getTableName('jda_connector_log');
 
            $connection->delete(
                $jdaLogTable,
                "created_at < date_sub(CURDATE(), INTERVAL ".$this->_helper->getLogsArchiveNumberOfDays()." Day)"
            );
        }
        return;
    }
}

就是这样,希望本文能以某种方式对您有所帮助。请留下您的评论,让我们知道您的想法?谢谢。

相关文章

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