As per the above link to overcome the Object Type Error I have to run the command php bin/magento s:d:c
after I run the command I got error like
.
Code
<?php
namespace Sathya\LoadProduct\Controller\Index;
use Magento\Framework\App\Action\Context; // constructor
use Magento\Framework\App\Action\Action; // Controller class
use Magento\Catalog\Model\Product; // Model Method
use Magento\Catalog\Api\ProductRepositoryInterface; // Repository Method
use Magento\Catalog\Model\ProductFactory; // Factory Method
use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct;
class Index extends Action
{
protected $productId = 500, $productModel, $_productFactory, $_productRepository,$ResourceProduct;
public function __construct(Context $context, Product $productModel,ResourceProduct $ResourceProduct,ProductFactory $_productFactory, ProductRepositoryInterface $_productRepository)
{
parent::__construct($context);
$this->productModel = $productModel;
$this->ResourceProduct = $ResourceProduct;
$this->_productFactory = $_productFactory;
$this->_productRepository = $_productRepository;
$this->getProductRepository();
$this->getProductModel();
$this->getProductFactory();
}
public function getProductModel()
{
$product = $this->productModel;
$this->ResourceProduct->load($product,$this->productId);
$product->setName("ram's phant");
$this->ResourceProduct->save($product);
echo $product->getName()."<br>";
}
public function getProductFactory()
{
$product = $this->_productFactory->create();
$this->ResourceProduct->load($product,$this->productId);
$product->setName("ram's phant");
$this->ResourceProduct->save($product);
echo $product->getName()."<br>";
}
public function getProductRepository()
{
$product = $this->_productRepository->getById($this->productId);
echo $product->getName()."<br>";
}
public function execute() { }
}