Question

I know we should use service contracts(Repository) over $model->load(), but in Repository itself, how to replace the deprecated load() function?

For example, in Magento\Cms\Model\PageRepository, the official Magento 2.1.9 used the deprecated load() function too.

public function getById($pageId)
{
    $page = $this->pageFactory->create();
    $page->load($pageId);
    if (!$page->getId()) {
        throw new NoSuchEntityException(__('CMS Page with id "%1" does not exist.', $pageId));
    }
    return $page;
}

Reference

When Should We Use a Repository and Factory in Magento 2?

Is there ever a reason to prefer $model->load() over service contracts?

Was it helpful?

Solution

You can use ResourceModel's load() instead, which extends Magento\Framework\EntityManager\EntityManager's load() method.

We could see a example in: Magento\Cms\Model\BlockRepository

public function getById($blockId)
    {
        $block = $this->blockFactory->create();
        $this->resource->load($block, $blockId);
        if (!$block->getId()) {
            throw new NoSuchEntityException(__('CMS Block with id "%1" does not exist.', $blockId));
        }
        return $block;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top