1

I'm trying to fix product descriptions, therefore i try to iterate through a collection and then save it.

But got exception here:

[ReflectionException]
Class Magento\Catalog\Model\Product\Attribute\Backend\Media does not exist

use Magento\Framework\App\State;

use \Magento\Framework\ObjectManagerInterface;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ProductRepository;


class CollectionFixCommand extends Command
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $productFactory;

    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    protected $productRepository;

    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $productModel;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    protected $collectionFactory;


    public function __construct(
        ProductFactory $productFactory,
        CollectionFactory $collectionFactory,
        ObjectManagerInterface $manager,
        ProductRepository $productRepository,
        State $state
    )
    {
        $state->setAreaCode('admin');
        $this->productFactory = $productFactory;
        $this->collectionFactory = $collectionFactory;
        $this->objectManager = $manager;
        $this->productRepository = $productRepository;

        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('collection:fix')->setDescription('Fixes product collection');
    }


    protected function execute(InputInterface $input, Outputinterface $output)
    {
        $collection = $this->collectionFactory->create();
        $collection->addFieldToSelect('*');

        foreach ($collection->getItems() as $product)
        {
            $description = $product->getData('description');

            //modifies description here

            $product->setData('description', $description);

            try{
                $this->productRepository->save($product);
            } catch(Exception $e){
                $output->writeln($e->getMessage());
            }
//          [ReflectionException]                                                       
//           Class Magento\Catalog\Model\Product\Attribute\Backend\Media does not exist 
        }

//      try{
//          $collection->save();
//      } catch(Exception $e){
//          $output->writeln($e->getMessage());

//      [ReflectionException]                                                       
//      Class Magento\Catalog\Model\Product\Attribute\Backend\Media does not exist 
//      }
    }
}

Any ideas anyone?

Full stack trace:

Exception trace:
 () at /vendor/magento/framework/Code/Reader/ClassReader.php:19
 ReflectionClass->__construct() at /vendor/magento/framework/Code/Reader/ClassReader.php:19
 Magento\Framework\Code\Reader\ClassReader->getConstructor() at /vendor/magento/framework/ObjectManager/Definition/Runtime.php:44
 Magento\Framework\ObjectManager\Definition\Runtime->getParameters() at /vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:71
 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /vendor/magento/framework/ObjectManager/ObjectManager.php:57
 Magento\Framework\ObjectManager\ObjectManager->create() at /vendor/magento/framework/Validator/UniversalFactory.php:36
 Magento\Framework\Validator\UniversalFactory->create() at /vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php:498
 Magento\Eav\Model\Entity\Attribute\AbstractAttribute->getBackend() at /var/generation/Magento/Catalog/Model/ResourceModel/Eav/Attribute/Interceptor.php:1103
 Magento\Catalog\Model\ResourceModel\Eav\Attribute\Interceptor->getBackend() at /vendor/magento/module-eav/Model/ResourceModel/ReadHandler.php:139
 Magento\Eav\Model\ResourceModel\ReadHandler->execute() at /vendor/magento/framework/EntityManager/Operation/Read/ReadAttributes.php:59
 Magento\Framework\EntityManager\Operation\Read\ReadAttributes->execute() at /vendor/magento/framework/EntityManager/Operation/Read.php:116
 Magento\Framework\EntityManager\Operation\Read->execute() at /vendor/magento/framework/EntityManager/EntityManager.php:61
 Magento\Framework\EntityManager\EntityManager->load() at /vendor/magento/module-catalog/Model/ResourceModel/Product.php:655
 Magento\Catalog\Model\ResourceModel\Product->load() at /var/generation/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php:258
 Magento\Catalog\Model\ResourceModel\Product\Interceptor->load() at /vendor/magento/framework/Model/AbstractModel.php:531
 Magento\Framework\Model\AbstractModel->load() at /vendor/magento/framework/Interception/Interceptor.php:146
 Magento\Catalog\Model\Product\Interceptor->___callPlugins() at /var/generation/Magento/Catalog/Model/Product/Interceptor.php:2301
 Magento\Catalog\Model\Product\Interceptor->load() at /vendor/magento/module-catalog/Model/ProductRepository.php:220
 Magento\Catalog\Model\ProductRepository->get() at /var/generation/Magento/Catalog/Model/ProductRepository/Interceptor.php:24
 Magento\Catalog\Model\ProductRepository\Interceptor->get() at /vendor/magento/module-catalog/Model/ProductRepository.php:493
 Magento\Catalog\Model\ProductRepository->save() at /vendor/magento/framework/Interception/Interceptor.php:74
 Magento\Catalog\Model\ProductRepository\Interceptor->___callParent() at /vendor/magento/framework/Interception/Chain/Chain.php:70
 Magento\Framework\Interception\Chain\Chain->invokeNext() at /vendor/magento/framework/Interception/Chain/Chain.php:63
 Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}() at /vendor/magento/module-configurable-product/Model/Plugin/AroundProductRepositorySave.php:62
 Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave->aroundSave() at /vendor/magento/framework/Interception/Chain/Chain.php:67
 Magento\Framework\Interception\Chain\Chain->invokeNext() at /vendor/magento/framework/Interception/Interceptor.php:138
 Magento\Catalog\Model\ProductRepository\Interceptor->Magento\Framework\Interception\{closure}() at /vendor/magento/module-catalog-inventory/Model/Plugin/AroundProductRepositorySave.php:74
 Magento\CatalogInventory\Model\Plugin\AroundProductRepositorySave->aroundSave() at /vendor/magento/framework/Interception/Interceptor.php:142
 Magento\Catalog\Model\ProductRepository\Interceptor->___callPlugins() at /var/generation/Magento/Catalog/Model/ProductRepository/Interceptor.php:52
 Magento\Catalog\Model\ProductRepository\Interceptor->save() at /src/Aqio/PMT/Console/Command/CollectionFixCommand.php:89
 Aqio\PMT\Console\Command\CollectionFixCommand->execute() at /vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257
 Symfony\Component\Console\Command\Command->run() at /vendor/symfony/console/Symfony/Component/Console/Application.php:874
 Symfony\Component\Console\Application->doRunCommand() at /vendor/symfony/console/Symfony/Component/Console/Application.php:195
 Symfony\Component\Console\Application->doRun() at /vendor/magento/framework/Console/Cli.php:96
 Magento\Framework\Console\Cli->doRun() at /vendor/symfony/console/Symfony/Component/Console/Application.php:126
 Symfony\Component\Console\Application->run() at /bin/magento:23
  • Although it is a late comment, have you checked clearing generated/code? – Kumar A. Sep 28 '18 at 8:01
3

The solution:

$product->setData('description', $description);
try{
    $product->getResource()->saveAttribute($product, 'description');
} catch(Exception $e){
    $output->writeln($e->getMessage());
}
2

You should use ProductRepositiry to save the product.

Save method in product model is deprecated and collection save method just iterate by item and call save method for them

  • Thanks for the advice, but i've got the exact same error there... Just added the whole code above! – Thomas Lindner Jun 26 '16 at 18:42
  • it's strange, can you post the full stack trace? (for command line add -vvv) – KAndy Jun 27 '16 at 11:33
  • you can find the full stack trace above! – Thomas Lindner Jun 27 '16 at 14:52
  • Just made a fresh install of 2.1.0 because upgrade from 2.0.7 to 2.1.0 broke too much stuff. Had some issues now with required attributes, just removed the requirement for testing purpose. And now i'm getting [Magento\Framework\Exception\NoSuchEntityException] Requested product doesn't exist – Thomas Lindner Jun 29 '16 at 20:14
  • It' looks like bug, can you create issue on github? – KAndy Jun 30 '16 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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