Magento – Error while trying to programatically create flat category

apicategorycategory-treemagento-1.9programmatically

Using Magento 1.9.0.1 I am trying to programmatically create categories using a data upgrade script with the flat categories option turned on.

This first category I want to create is a new root category.

My data upgrade script looks something like this

$root_cat = array( ..Root Cat Data.. );

$store = Mage::app()->getStore();

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addIsActiveFilter();

$all_store_cat = array();

foreach ($categories as $_category) 
{
    $all_store_cat[] = $_category->getName();
}

if ( in_array($root_cat['name'], $all_store_cat) )
{
    echo 'Public Catalog already exists';
}
else
{
    echo 'Does not exist, lets create';

    $category = Mage::getModel('catalog/category');
    $category->setStoreId(Mage::app()->getStore()->getId());

        $data['path'] = '1'; // This is the path for root categorys
        // $data['setPosition'] = $root_cat['position'];

        $data['name'] = $root_cat['name'];
        $data['is_active'] = $root_cat['is_active'];

        $data['description'] = $root_cat['description'];

        $data['meta_title'] = $root_cat['meta_title'];
        $data['meta_keywords'] = $root_cat['meta_keywords'];
        $data['meta_description'] = $root_cat['meta_description'];
        $data['include_in_menu'] = $root_cat['include_in_menu'];

        $data['display_mode'] = $root_cat['display_mode'];
        $data['is_anchor'] = $root_cat['is_anchor'];

    $category->addData($data);

    // print_r($category);

    // die();

    $category->save();

    // unset($category);

    echo "created: " . $root_cat['name'] . "<br>";
}

I get the following error

a:5:{i:0;s:479:"Error in file: "......path....../data-upgrade-1.0.1-1.0.2.php" - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`DBNAME`.`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` (`entity_id`) ON)";i:1;s:952:"#0 ......path....../app/code/core/Mage/Core/Model/Resource/Setup.php(645): Mage::exception('Mage_Core', 'Error in file: ...')
#1 ......path....../app/code/core/Mage/Core/Model/Resource/Setup.php(407): Mage_Core_Model_Resource_Setup->_modifyResourceDb('data-upgrade', '1.0.1', '1.0.2')
#2 ......path....../app/code/core/Mage/Core/Model/Resource/Setup.php(286): Mage_Core_Model_Resource_Setup->_upgradeData('1.0.1', '1.0.2')
#3 ......path....../app/code/core/Mage/Core/Model/Resource/Setup.php(269): Mage_Core_Model_Resource_Setup->applyDataUpdates()
#4 ......path....../app/code/core/Mage/Core/Model/App.php(351): Mage_Core_Model_Resource_Setup::applyAllDataUpdates()
#5 ......path....../app/Mage.php(684): Mage_Core_Model_App->run(Array)
#6 ......path....../index.php(87): Mage::run('', 'store')
#7 {main}";s:3:"url";s:1:"/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}

Any help greatly appreciated, pulling my hair out over this one!

Best Answer

Ok I think I got it.

Replacing..

$store = Mage::app()->getStore();

..with..

$store = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

..seems to have done the trick.

Related Topic