Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Can’t save product with custom script. 
 
blakew
Sr. Member
 
Total Posts:  105
Joined:  2008-06-20
Dallas, TX
 

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

 Signature 

Organic Internet & Mobile
Mobile & Web Application Design & Development
Yorkshire, UK

 
Magento Community Magento Community
Magento Community
Magento Community
 
jemoon
Sr. Member
 
Avatar
Total Posts:  127
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();

 Signature 

Wszystkich zainteresowanych zapraszam na Nieoficjalne, Polskie Forum Magento.
Jedno z wdrożeń:  Kosmetyki

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  105
Joined:  2008-06-20
Dallas, TX
 

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:  341
Joined:  2008-11-14
Spain
 
Matt Dean - 10 April 2009 02: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!

 Signature 

amartinez {web/developer/designer} free modules: CustomImportExport Like it? Donate!

 
Magento Community Magento Community
Magento Community
Magento Community
 
amartinez
Guru
 
Avatar
Total Posts:  341
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!

 Signature 

amartinez {web/developer/designer} free modules: CustomImportExport Like it? Donate!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Gabriel Queiroz
Sr. Member
 
Total Posts:  134
Joined:  2008-05-28
Copenhagen, Denmark
 
Matt Dean - 10 April 2009 02: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,

 Signature 

--
Flow eCommerce Ajax Catalog - Edit your products’ details without leaving the product grid.
http://flowecommerce.com
Farmacia Online Viva Farma

 
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 02: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.

 Signature 

http://www.ikonetics.com/iphone_apps/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
704029 users|1824 users currently online|497358 forum posts