In Repository, how to replace deprecated load() function?
-
19-12-2020 - |
Вопрос
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?
Решение
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;
}
Не связан с magento.stackexchange