3

I am currently implementing a SaveHandler class for my custom model like described here. My goal is to save the relations between my custom model and the Store model.

There \Magento\Framework\EntityManager\EntityMetadataInterface::getEntityConnection is used in order to get a connection. This method is deprecated since 100.1.0 but there's no alternative provided.

Is there any alternative known?

I've been looking around in core classes and in the SaveHandler for pages and blocks it is still used.

1 Answer 1

1

A little bit too late, but hopefully will help someone.

I has the same problem while reproducing the Cms Block SaveHandler behavior. Instead of using the deprecated \Magento\Framework\EntityManager\EntityMetadataInterface::getEntityConnection, I have used the Magento\Framework\App\ResourceConnection

<?php

namespace YourVendorName\YourModule\Model\ResourceModel\YourEntity\Relation\Store;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;

class SaveHandler implements ExtensionInterface
{
    private ?AdapterInterface $connection = null;
    private ResourceConnection $resourceConnection;

    /**
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(
        ResourceConnection $resourceConnection
    ) {
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * @inheritDoc
     */
    public function execute($entity, $arguments = [])
    {
        (...)
        $connection = $this->getConnection();
        (...)
    }

    /**
     * Connects to the database
     *
     * @return AdapterInterface
     */
    private function getConnection(): AdapterInterface
    {
        if (!$this->connection) {
            $this->connection = $this->resourceConnection->getConnection('core_write');
        }
        return $this->connection;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.