Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't update products attribute in CLI #5414

Closed
developer-lindner opened this issue Jul 2, 2016 · 14 comments
Closed

Can't update products attribute in CLI #5414

developer-lindner opened this issue Jul 2, 2016 · 14 comments

Comments

@developer-lindner
Copy link

developer-lindner commented Jul 2, 2016

Steps to reproduce

  1. Install Magento from 2.1 branch.
  2. Have some product data (sample data for example)
  3. Run CLI Command (code below)
ini_set('memory_limit', '2048M');

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');

            //modify description here

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

            try{
                $this->productRepository->save($product);
            } catch(Exception $e){
                $output->writeln($e->getMessage());
            }
        }
    }
}

Expected result

  1. Product Attribute gets updated & saved

Actual result

  1. Error message appears:
    " [Magento\Framework\Exception\CouldNotSaveException]
    Unable to save product "

Furthermore

IF AreaCode is set as state the command "works" but setup:upgrade doesn't works anymore:

"[Magento\Framework\Exception\LocalizedException]
Area code already set"

IF AreaCode is not set, the command doesn't work:

"[Magento\Framework\Exception\SessionException]
Area code not set: Area code must be set before starting a session."

"[Magento\Framework\Exception\LocalizedException]
Area code is not set "

But setup:upgrade works again.

How can i have both working as expected?!

@developer-lindner developer-lindner changed the title Can't save products attribute change Can't update products attribute in CLI Jul 8, 2016
@developer-lindner
Copy link
Author

developer-lindner commented Jul 8, 2016

Now after i've fixed the visibility of products in frontend (catalog search index didn't pass probably, changed mysql settings) the "CouldNotSave" exception from above is gone.

Instead i get this now:

 [Exception]                                                                                                                                                            
  Warning: Error while sending QUERY packet. PID=61395 in vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 228

Which means the code above creates a huge data set which is not chunked and therefore way too large for mysql to handle it, right?

Any ideas how to get around?

@developer-lindner
Copy link
Author

I've tweaked the wait_timeout setting in my mysql config which "fixed" the exception "Error while sending QUERY packet". Now it's

[Magento\Framework\Exception\CouldNotSaveException]
Unable to save product 

again...

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

you are setting the area to 'admin'. does it work if you set it to 'adminhtml'?
$state->setAreaCode('adminhtml');

@developer-lindner
Copy link
Author

developer-lindner commented Jul 14, 2016

@bh-ref well, seems like AreaCode should be 'adminhtml', but it doesn't fixes the Magento\Framework\Exception\LocalizedException from above (which stops setup:upgrade from working).

The main issue is saving the product's attribute change where i still struggle.

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

my experiments with using the productrepository in a script to use it to change attribute values on many products (just like you are trying) was very disappointing. I use different store views (for different languages), and the get messed up when I used the productrepository.

what I am doing now is using the import mechanism to change attribute values, it does not seem to have these nasty side effects. there are two ways of doing it.

first way: you prepare the csv file with the minimum required fields (you have to experiment a little)

second way: you are simulating a csv by using arrays with this module: https://github.com/firegento/FireGento_FastSimpleImport2.

maybe you should start with csv first. go to admin -> system -> import, choose product import and download the example csv and experiment with that.

@developer-lindner
Copy link
Author

@bh-ref thanks for the hints! Is there no chance to get it work?
All i want to do is replace absolute urls with the media directive ( {{media url=""}} )... not importing everything again.

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

I can feel your pain ;)

it should be easy to do these things, I know, but I didn't managed it to do it by code without nasty side effects. another way would be to use SQL (I know, I know).

by the way, the import does not need all fields for existing products. I think store_view_code (leave that empty) and sku are the bare minimum for existing products. you would then add all additional columns you may need.

@developer-lindner
Copy link
Author

developer-lindner commented Jul 14, 2016

Good to know that someone else went through this hell also :)

In Magento there was a very performant way of doing this:

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

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

it's frustrating, and since magento is now overhelmed with issues, you have to wait quite a bit for an answer.

I did not work with magento 1 too much, but that was sure a nice and simple way to do it.

if you go for SQL, this may help: http://magento-quickies.alanstorm.com/post/144213777050/running-arbitrary-sql-in-magento-2-like-an

good luck with fixing your problem :)

@developer-lindner
Copy link
Author

developer-lindner commented Jul 14, 2016

I'll try a bit more... seems like the product's ResourceModel got a saveAttribute function...

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

if you come up with a working solution like that I eager to hear it. fingers crossed ;)

@developer-lindner
Copy link
Author

developer-lindner commented Jul 14, 2016

@bh-ref the solution is right above! ;)

The issue with the AreaCode is still there, but i don't care... this command is just a temporarily fix option.

@bh-ref
Copy link
Contributor

bh-ref commented Jul 14, 2016

thanks for posting back, I will give it a shot if I come across a similar problem next time

@developer-lindner
Copy link
Author

There is another issue with directives... so even when i got the solution now i can't use it.
See #3860

magento-cicd2 pushed a commit that referenced this issue Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants