0

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!

3 Answers 3

1

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.

0

Create category dynamically:

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

$cat['name'] = "Custom Category Name here";
$cat['path'] = "1/2/30"; //parent relationship..
$cat['description'] = "categorie's description";
$cat['is_active'] = 1;
$cat['is_anchor'] = 0; //for layered navigation
$cat['page_layout'] = 'two_columns_left';
$cat['url_key'] = "custom-category"; //url to access this category
$cat['image'] = "custom-category.jpg";

$category->addData($cat);
$category->save();

Then reindex catalog_category_flat dynamically:

$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat');
$process->reindexEverything();
0

I fixed the issue by replacing

$app = Mage::app(0);

with

Mage::app("admin");

in my import script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.