Magento - How to program a new parent for a category?

I use this method to create new categories:

private function createCat($name){
    $category = Mage::getModel( 'catalog/category' );
    $category->setStoreId( 0 );
    $category->setName( $name ); // The name of the category
    $category->setUrlKey( strtolower($name) ); // The category URL identifier
    $category->setIsActive( 1 ); // Is it enabled?
    $category->setIsAnchor( 0 );
    // Display mode can be 'PRODUCTS_AND_PAGE', 'PAGE', or 'PRODUCTS'
    $category->setDisplayMode( 'PRODUCTS' );
    $category->setPath( '1/3' ); // Important you get this right.
    $category->setPageTitle( $name );
    $category->save();

    return $category->getId();
}

After I know the identifier assigned by Magento to this category, I then call the following loop in a loop to assign each category a parent category:

private function assignCat($id, $parent){

    $category = Mage::getModel( 'catalog/category' )->load($id);
    $category->setPath( '1/3/'.$parent.'/'.$id ); // Important you get this right.
    $category->save();
    return;
}

However, it does not work. The first method creates the categories perfectly, but after starting the second method I can’t even load the admin panel to show the categories.

What am I doing wrong?

EDIT:

. , catalog_category_entity, . - , , 0 . - , ?

: :

. , move() :

private function assignCat($id, $parent){
    $category = Mage::getModel( 'catalog/category' )->load($id);
    Mage::unregister('category');
    Mage::unregister('current_category');
    Mage::register('category', $category);
    Mage::register('current_category', $category);
    $category->move($parent);
    return;
}
+5
1

setParentId():

$category->setParentId($parentCategoryId);

Category Api Model :

$categoryData = array(
    'name'        => $name,
    'url_key'     => $urlKey,
    'is_active'   => '1',
);

$categoryApi = Mage::getModel('catalog/category_api_v2');
$categoryId = $categoryApi->create($parentCategoryId, $categoryData, $store)
+1

All Articles