Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Can’t save product with custom script. 
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I have a very simple script:

<?php
require_once("/var/www/magento/app/Mage.php");
Mage::app();

$product Mage::getModel('catalog/product');

$product->load(1);

echo 
$product->getName();

$product->save();

?>

I get this when I run the script. This script is extremely simple and I can’t figure out what Magento changed to make this not work. I have been using load and save without any problems since I started using magento at the release of 1.0

PHP Fatal error:  Uncaught exception 'Exception' with message 'Warning: Invalid argument supplied for foreach()  in /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 937' in /var/www/magento/app/code/core/Mage/Core/functions.php:246
Stack trace
:
#0 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(937): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/magent...', 937, Array)
#1 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(892): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php(251): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 /home/blakew/scripts/testSave.php(12): Mage_Core_Model_Abstract->save()
#4 {main}
  
thrown in /var/www/magento/app/code/core/Mage/Core/functions.php on line 246

Perhaps I am not setting something that is now required. I have tried settings storeId, etc… but nothign is working. line 937 of the abstract.php file is a variable called $origData and its an invalid argument. I have traced that back to the mage core classes, but Magento is so abstract that I don’t have my head wrapped around it enough to know whats going on. Any ideas?

I have tried it on different servers even and I have the exact same problem with 1.3

 
Magento Community Magento Community
Magento Community
Magento Community
 
Matt Dean
Guru
 
Avatar
Total Posts:  549
Joined:  2008-05-27
 

Hi Blake,

Try replacing:

Mage::app();
with

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Does that help?

Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
jemoon
Sr. Member
 
Avatar
Total Posts:  128
Joined:  2008-09-30
Gdansk, Poland
 

Hi,
@blakew or maybe try to paste something like:

require_once("/var/www/magento/app/Mage.php");
umask(0);
Mage::app();
 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

Matt and Jemoon, thanks so much for your efforts. Matt your suggestion worked great. I believe there are quite a few little tricks like this that I am not aware of. Again, thanks so much.

 
Magento Community Magento Community
Magento Community
Magento Community
 
amartinez
Guru
 
Avatar
Total Posts:  347
Joined:  2008-11-14
Spain
 
Matt Dean - 10 April 2009 01:27 PM

Hi Blake,

Try replacing:

Mage::app();
with

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Does that help?

Matt

yes! Yes! YES! This work!!!! THANKS A LOT!!!

(I hate the magento framework)
(I love the magento framework)

s2,

UPDATE: the configurable products don’t link to their simple items :( any ideas? thanks again!

 
Magento Community Magento Community
Magento Community
Magento Community
 
amartinez
Guru
 
Avatar
Total Posts:  347
Joined:  2008-11-14
Spain
 

autoresponse: my script import now configurable & grouped products

get it at http://www.magentocommerce.com/boards/viewreply/133503/

hope it help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Gabriel Queiroz
Sr. Member
 
Total Posts:  135
Joined:  2008-05-28
Brasília, Brazil
 
Matt Dean - 10 April 2009 01:27 PM

Hi Blake,

Try replacing:

Mage::app();
with

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Does that help?

Matt

Matt, thanks a lot, this made my code work like a charm. =)

Just out of curiosity, does anyone have the slightest idea of why we can only save products when Mage is set to the admin store view?

It kind of makes sense, given that product updates are only made on the admin side, but still… by the way the code architecture is placed, this doesn’t sound like a real limitation, but sounds more like a bug…

Cheers,

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mateusz Sobczak
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2009-07-21
BC, Canada
 

Hi,

I would like to change one product’s attribute just for a store view (not the default value). How do I do it then?

I tried:

Mage::app()->setCurrentStore('ad_view_ca');

before setting up the attribute but it gives me a warning:

Warning: Invalid argument supplied for foreach() in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 970

I also tried:

Mage::app()->setCurrentStore(2);

The result is the same.

Without setting up current store I can only update the default value.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dougboberg
Jr. Member
 
Total Posts:  2
Joined:  2009-09-26
 
Matt Dean - 10 April 2009 01:27 PM

Hi Blake,

Try replacing:

Mage::app();
with

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Does that help?

Matt

I’m completely speechless… I spent two days of fruitless googling… and THIS IS THE ANSWER.  I don’t know WHY it works, I don’t CARE why it works.  The point is that IT WORKS.

Thank you Matt.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nedoKivi
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2009-11-15
Israel
 
Matt Dean - 10 April 2009 01:27 PM

Hi Blake,

Try replacing:

Mage::app();
with

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Does that help?

Matt

you Sir are a genius.. Thank you so much

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top