Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load model from DB in Magento 2
<?php
class SomeClass
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Product
*/
private $productResource;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
private $productFactory;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product $productResource,
\Magento\Catalog\Model\ProductFactory $productFactory
)
{
$this->productResource = $productResource;
$this->productFactory = $productFactory;
}
/**
* @return \Magento\Catalog\Model\Product
*/
public function getProduct()
{
$productId = 1;
/** @var $product \Magento\Catalog\Model\Product */
$product = $this->productFactory->create();
$this->productResource->load($product, $productId);
return $product;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.