cancel
Showing results for 
Search instead for 
Did you mean: 

Extension doesn't work unless I add plugin to /app/etc/di.xml file

Extension doesn't work unless I add plugin to /app/etc/di.xml file

My extension works if I add the plugin to the main app/etc/di.xml right before another plugin that is called, but doesn't work as defined in my Plugin/etc/di.xml

portion of app/etc/di.xml

<type name="Magento\Framework\DB\Adapter\AdapterInterface">
        <plugin name="add_sql_sessions" type="Truelight\SqlSessions\AddSqlSessions" sortOrder="1"/>
        <plugin name="execute_commit_callbacks" type="Magento\Framework\Model\ExecuteCommitCallbacks" />
    </type>

The issues is that file gets regenerated and my changes are lost anytime a composer:install is run.  My plugin is below.  Can anyone help me understand why my plugin isn't be called globally, unless I add my plugin to the apps main di.xml file?  Also, is there anyway to ensure my plugin get installed first before any non-magento plugins?  I need to set a local mysql sessions variable to get around my managed hosts global settings, and and need to make sure my plugin is always installed before other plugins are added.  This issue would come up during an initial install(If another plugin was installed and tried to create/modify a table) in my case.

 

AddSqlSessions.php

<?php
namespace Truelight\SqlSessions;

use Magento\Framework\DB\Adapter\AdapterInterface;

/**
 * Add Sql sessions variables afterSetup
 */
class AddSqlSessions
{

    public function afterStartSetup(AdapterInterface $subject, AdapterInterface $result): AdapterInterface
    {
        $subject->query('SET @@local.sql_require_primary_key = OFF');
        return $result;
    }
}

composer.json

{
    "name": "truelight/sqlsessions",
    "description": "Add Sql Sessions",
    "require": {
        "php": "^7.1.0"
    },
    "type": "magento2-module",
    "version": "1.6.6",
    "license": [
        "Commercial"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Truelight\\SqlSessions\\": ""
        }
    }
}

registration.php

<?php
/**
 * @author True Light
 * @copyright Copyright (c) 2020 True Light, LLC
 * @package Truelight_SqlSessions
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Truelight_SqlSessions',
    __DIR__
);

etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * @author True Light
 * @copyright Copyright (c) 2020 True Light, LLC
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\DB\Adapter\AdapterInterface">
        <plugin name="add_sql_sessions" type="Truelight\SqlSessions\AddSqlSessions" sortOrder="1"/>
    </type>
</config>

etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * @author True Light
 * @copyright Copyright (c) 2020 True Light, LLC
 * @package Truelight_SqlSessions
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Truelight_SqlSessions" setup_version="1.6.6">
        <sequence>
            <module name="Magento_Store" />
        </sequence>
    </module>
</config>
1 REPLY 1

Re: Extension doesn't work unless I add plugin to /app/etc/di.xml file

if you want to not add plugin remove di.xml code then it will be work fine.