1

I'm performing an upgrade to Magento 2.4 and the registry class has been deprecated but it's being used in some custom modules, please see the code below:

<?php declare(strict_types=1);

namespace TP\Cars\Model;

use TP\Cars\Model\ResourceModel\Facility\Collection;
use TP\CarsApi\Api\Data\FacilityInterface;
use TP\CarsApi\Api\Data\FacilityInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;

class Facility extends AbstractModel
{
    protected $_eventPrefix = 'TP_vehicle_facility';

    private DataObjectHelper $dataObjectHelper;

    private FacilityInterfaceFactory $facilityDataFactory;

    /**
     * @param array<mixed> $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FacilityInterfaceFactory $facilityDataFactory,
        DataObjectHelper $dataObjectHelper,
        ResourceModel\Facility $resource,
        Collection $resourceCollection,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $resource,
            $resourceCollection,
            $data
        );
        $this->facilityDataFactory = $facilityDataFactory;
        $this->dataObjectHelper = $dataObjectHelper;
    }

    public function getDataModel(): FacilityInterface
    {
        $facilityData = $this->getData();

        $facilityDataObject = $this->facilityDataFactory->create();
        $this->dataObjectHelper->populateWithArray(
            $facilityDataObject,
            $facilityData,
            FacilityInterface::class
        );

        return $facilityDataObject;
    }
}

Can you suggest how I can eliminate the use of the registry class?

1
  • 1
    I think in above code you can ignore, because your code directly not using Registry. it is just using in parent class which is magento code.
    – Pawan
    Aug 10, 2023 at 5:33

2 Answers 2

0

This is the link That will definitely helps you, this explains very well.

https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/

0
0

You don't use the registry in this example. The registry is received as a constructor dependency and just passed to the parent constructor.

You have no way to avoid registry receiving here, as the parent class requires it.

But in this particular case, you don't need to avoid it. Magento will keep this class existing in the next versions because of such cases as yours. All the children of AbstractModel request the Registry, so they cannot just remove it and break all the custom code.

I think they will make it nullable, and your code will still work.


About the Registry alternatives.

Usually, registry usage is needed to receive the current Product instance or Category instance. You can easily replace it by getting the current Product or Category identifier using the Request instance.

But please be aware. You must have a Request as a dependency only in the Controller layer. Then you need to pass the requested object (or identifier) to some separate service as a parameter.

Your Answer

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

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