help with assignCollectionAttribute()

Permalink
Hello community, I am trying to run this code on a package installer but keep getting errors.
Has anyone ever done this before?
$pageType = CollectionType::getByHandle('home');
         $ak= CollectionAttributeKey::getByHandle('category_image');
         $pageType->assignCollectionAttribute($ak);

PauloCarvalhoDesign
 
Mnkras replied on at Permalink Reply
Mnkras
$cab1 = CollectionAttributeKey::getByHandle('blag');
$page = CollectionType::getByHandle('blah');
$page->assignCollectionAttribute($cab1);


should work, what are the errors
PauloCarvalhoDesign replied on at Permalink Reply
PauloCarvalhoDesign
Hi Mnkras actually I have manage how to do it.
I also manage to figure out how to make attributes custom or not by default in to any pagetype installed from package.
I have posted a how-to that is under revision I think!
wildapple replied on at Permalink Reply
wildapple
Sorry to bump such an old thread but I've never bothered doing this until recently and using this method I'm finding that if you install a package with this in it, and the page type being referenced already has the attribute(s) assigned to it, it will throw an error on install, looks something like this:

mysqli error: [1062: Duplicate entry '4-1' for key 'PRIMARY'] in EXECUTE("insert into PageTypeAttributes (ctID, akID) values ('4', '1')")


I'm sure there's a way to check if the page already has the attribute assigned on install so this doesn't happen but I'm not sure what that "if" statement would look like because I'm not even sure what I would check against?