Created
October 25, 2018 20:28
-
-
Save herveguetin/da8727f9cff6dda8f060cc742eaed0a4 to your computer and use it in GitHub Desktop.
Load model from DB in Magento 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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