0

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?

3

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;
    }
  • Thanks, accept and my vote ran out today. repositoryModel? It should be ResourceModel, right? – Key Shang Dec 4 '17 at 3:13
  • ResourceModel's load() which extends Magento\Framework\EntityManager\EntityManager's load() method – Key Shang Dec 4 '17 at 3:21
  • yeah, that was my bad. can say a typo :) – Yogesh Agarwal Dec 4 '17 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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