0

Object Type Error enter image description here

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 Screen Shots . 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()   {    }
}
4
  • did you try to remove generated dir?
    – ASQ
    Mar 16, 2020 at 11:05
  • no, never I tried
    – Sathya
    Mar 16, 2020 at 12:07
  • namespace Sathya\LoadProduct\Controller\Index; this path is incorrect it should be namespace Sathya\LoadProduct\Index
    – ASQ
    Mar 17, 2020 at 6:08
  • Thanks for your interest, I got the solution. @ASQ
    – Sathya
    Mar 17, 2020 at 7:05

1 Answer 1

0

Remove the generated directory by running the following command

rm -rf generated/

EDIT

Your namespace path is incorrect it should be

namespace Sathya\LoadProduct\Index
3
  • still, the same error repeating.
    – Sathya
    Mar 16, 2020 at 12:36
  • can you please share your code, i think you had given the wrong path
    – ASQ
    Mar 16, 2020 at 12:42
  • yeah sure as of now I'm in learning phase so I tried to use all the method to load the data from table.
    – Sathya
    Mar 16, 2020 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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