Question with installing select user attribute with a package

Permalink 3 users found helpful
I am trying to install a select user attribute in my package controller. I can get the actual attribute to install without values, but I'd rather have everything automated. My code so far is:

$selectAttr = AttributeType::getByHandle('select');
$genderAttr = UserAttributeKey::getByHandle('gender');
      if( !is_object($genderAttr) ) {
         UserAttributeKey::add($selectAttr, array('akHandle' => 'gender', 'akName' => t('Gender'), 'akSelectValues' => array('Male', 'Female'), 'akIsSearchable' => FALSE, 'uakProfileDisplay' => TRUE, 'uakMemberListDisplay' => FALSE, 'uakProfileEdit' => TRUE, 'uakRegisterEdit' => FALSE, 'akCheckedByDefault' => FALSE), $pkg);
      }


I am also trying to install the profile_private_messages_enabled and profile_private_messages_notification_enabled checkboxes for the user private message attributes to be installed. I'm confused about the error I get when I install the package because the attributes are not available in the dashboard. My code to install these attributes is as follows:

$checkboxAttr = AttributeType::getByHandle('boolean');
      $receivePMAttr = UserAttributeKey::getByHandle('profile_private_messages_enabled');
      if( !is_object($receivePMAttr) ) {
         UserAttributeKey::add($checkboxAttr, array('akHandle' => 'profile_private_messages_enabled', 'akName' => t('I would like to receive private messages.'), 'akIsSearchable' => FALSE, 'uakProfileDisplay' => TRUE, 'uakMemberListDisplay' => FALSE, 'uakProfileEdit' => TRUE, 'uakRegisterEdit' => FALSE, 'akCheckedByDefault' => TRUE), $pkg);
      }
      $receiveEmailAttr = UserAttributeKey::getByHandle('profile_private_messages_notification_enabled');
      if( !is_object($receiveEmailAttr) ) {
         UserAttributeKey::add($checkboxAttr, array('akHandle' => 'profile_private_messages_notification_enabled', 'akName' => t('Send me email notifications when I receive a private message.'), 'akIsSearchable' => FALSE, 'uakProfileDisplay' => TRUE, 'uakMemberListDisplay' => FALSE, 'uakProfileEdit' => TRUE, 'uakRegisterEdit' => FALSE, 'akCheckedByDefault' => TRUE), $pkg);
      }


This is the error that I get when I install the package.

mysql error: [1062: Duplicate entry 'profile_private_messages_enabled-2' for key 'akHandle'] in EXECUTE("insert into AttributeKeys (akHandle, akName, akIsSearchable, akIsSearchableIndexed, akIsAutoCreated, akIsEditable, atID, akCategoryID, pkgID) values ('profile_private_messages_enabled', 'I would like to receive private messages.', 0, 0, 0, 1, '3', '2', '24')")

Thanks for any help!

 
rockface replied on at Permalink Reply
rockface
Very good question!

According to the UserAttributeKey Class Reference (http://docs.mnkras.com/class_user_attribute_key.html) there is no "akSelectValues" member.

As you can see from this print_r of the returned object class, the values are nowhere in sight:
UserAttributeKey Object
(
    [searchIndexFieldDefinition:protected] => uID I(11) UNSIGNED NOTNULL DEFAULT 0 PRIMARY
    [error] => 
    [akID] => 37
    [akHandle] => gender
    [akName] => Gender
    [akCategoryID] => 2
    [akIsEditable] => 1
    [akIsSearchable] => 1
    [akIsSearchableIndexed] => 0
    [akIsAutoCreated] => 0
    [akIsColumnHeader] => 0
    [atID] => 8
    [atHandle] => select

So, unless someone has a better solution, you could hand jam the values like this:
Loader::model('user_attributes');
$db = Loader::db();
$selectAttr = AttributeType::getByHandle('select');
$genderAttr = UserAttributeKey::getByHandle('gender');
if( !is_object($genderAttr) ) {
    $genderObj = UserAttributeKey::add($selectAttr, array('akHandle' => 'gender', 'akName' => t('Gender'), 'akSelectValues' => array('Male', 'Female'), 'akIsSearchable' => FALSE, 'uakProfileDisplay' => TRUE, 'uakMemberListDisplay' => FALSE, 'uakProfileEdit' => TRUE, 'uakRegisterEdit' => FALSE, 'akCheckedByDefault' => FALSE), $pkg);
    $akID = $genderObj->akID;
    $db->Execute("INSERT INTO `atSelectOptions` (`akID`, `value`, `displayOrder`) 
       VALUES($akID, 'male',   0), ($akID, 'female', 1);");   
}
mended replied on at Permalink Reply
Thank you so much! That works perfectly. Also I figured out my second question, the atID for the private messaging attributes was NULL for both of them in my DB table for some reason. All is well now!
rockface replied on at Permalink Reply
rockface
Thanks... You helped me too! I was struggling with UserAttributeKey::add and you helped me get that syntax right.
olliephillips replied on at Permalink Best Answer Reply
olliephillips
Thanks for this thread guys, helped me alot.

For anyone else looking at doing this, the select options can also be set like this using the API rather than SQL.

$paymentSelect = UserAttributeKey::add($selectAttrType, array('akHandle' => 'payment_method', 'akName' => t('Payment Method'), 'akIsSearchable' => FALSE, 'uakProfileDisplay' => FALSE, 'uakMemberListDisplay' => FALSE, 'uakProfileEdit' => FALSE, 'uakRegisterEdit' => FALSE, 'akCheckedByDefault' => FALSE), $pkg);
         ## Add Options
         SelectAttributeTypeOption::add( $paymentSelect, 'Paypal', 0, 0);    
         SelectAttributeTypeOption::add( $paymentSelect, 'Cheque', 0, 0);