Programmatically create new attribute set while inheriting from another

After trying out again, messing up a lot and finally finding the correct answer, this works:

$entityTypeId = Mage::getModel('catalog/product')
                  ->getResource()
                  ->getEntityType()
                  ->getId(); //product entity type

$attributeSet = Mage::getModel('eav/entity_attribute_set')
                  ->setEntityTypeId($entityTypeId)
                  ->setAttributeSetName("test_set");

$attributeSet->validate();
$attributeSet->save();

$attributeSet->initFromSkeleton($entityTypeId)->save();

You need to save before you do initFromSekeleton(). Otherwise it just won't work.


See how the attribute sets are created (inherited) from the backend. Check this method: Mage_Adminhtml_Catalog_Product_SetController::saveAction().
In that method there is this line that 'clones' the attribute set:

$model->initFromSkeleton($this->getRequest()->getParam('skeleton_set')); 

Where ->getParam('skeleton_set') is the attribute set to be cloned. Basically you need to do something like this:

$cloneSetId = 4;//or anything else
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); //product entity type
$model = Mage::getModel('eav/entity_attribute_set'); //instantiate the model
$model->setEntityTypeId($entityTypeId);//attribute set is used for products
$model->setAttributeSetName('Attribute set name here');//set the attribute set name
$attributeSet->save(); // save before initFromSkeleton
$model->initFromSkeleton($cloneSetId);//clone one attribute set
$model->save();//save the new attribute set
//do other modifications here

Just a note, it appears that the attribute set API model can do this itself:

$newSetName    = 'My New Set';
$originalSetId = 4; // the set to base the new one off

/** @var Mage_Catalog_Model_Product_Attribute_Set_Api */
Mage::getModel('catalog/product_attribute_set_api')
    ->create($newSetName, $originalSetId);