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

Error While creating Category - Magento ver. 1.6.2.0
 
ramanzeon
Jr. Member
 
Total Posts:  5
Joined:  2012-10-09
 

Hello Pals,

I am facing problem while creating category by code in Magento 1.6.
However my code works on Magento EE.

Below is my code,
$catModel = Mage::getModel(’catalog/category’);
$catModel->load();

$data[’entity_id’] = “”;
$data[’parent_id’] = 4;
$data[’path’] = ‘1/2’;
$data[’name’] = “Mike World”;
$data[’url_key’] = “digital-titles”;
$data[’url_path’] = “mike-world.html”;

$catModel->setData($data);


$catModel->save();

But I am getting error, as below :
a:5:{i:0;s:313:"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
(`magento_test`.`catalog_category_flat_store_1`, CONSTRAINT `FK_CAT_CTGR_FLAT_STORE_1_ENTT_ID_CAT_CTGR_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_category_entity` (`enti)";i:1;s:1884:"#0 D:\wamp\www\test\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

Any help will be appreciated !!!

Thanks,

 
Magento Community Magento Community
Magento Community
Magento Community
 
GregorMendel
Jr. Member
 
Total Posts:  1
Joined:  2012-06-07
 

You probably don’t have flat categories enabled in EE so your code is working over there.

To anyone still suffering: you’ll have to set the store to the admin one. Else Magento uses a different model on subsequent calls leading to errors like this one and some more.

Solution:

Initialize your app this way

Mage::app() -> setCurrentStore Mage_Core_Model_App::ADMIN_STORE_ID );
instead of just doing
Mage:app();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top