0

Need help trying to remove a array value (Customer ID) from a attribute array value in a collection

 //unfollowAction
 //Get Model to save data to.
 $model = Mage::getModel("userprofiles/userprofiles");
 $model->getCollection()->addFieldToFilter('userprofiles_id', $id);
 $model->setUserprofilesId($id);

 //Get Model to fetch data from
 $model2 = Mage::getModel("userprofiles/userprofiles")->load($id);

 //Build Array output is 123,234,345,456,
 $FollowProfilesArray = array($model2->getFollowProfiles());

 //Remove specified ID from array, $profile_id equals current customer ID
 $remove_from_array = array_diff($FollowProfilesArray, array($profile_id,));

  foreach($remove_from_array as $key => $value){
  $select .= ''.$value.',';
  }

   //save new array to attribute value with the specified ID removed.
   $model->setFollowProfiles($select);

Problem is that this code is not removing the specified ID and saving the attribute value as 123,234,345,,

Note:

$model2->getFollowProfiles() output/saved value is 123,234,345,456 etc

the code $FollowProfilesArray = array($model2->getFollowProfiles()); does not work as stated above but if it hardcode the array data to

//Works,but not ideal or relevant
$FollowProfilesArray = array(123,234,345,456,);

So is there something im missing like exploding or imploding the $FollowProfilesArray = array($model2->getFollowProfiles()); code

1

Ok sorted it out yeah. Code below.


changed

$FollowProfilesArray = array($model2->getFollowProfiles()); 

to

//explode out the attribute value
$FollowProfilesArray = explode(",","".$model2->getFollowProfiles()."");

and changed

foreach($remove_from_array as $key => $value){
$select .= ''.$value.',';}

to

    //so if $value is NULL save no value id, stoppped the adding of , to the attribute value

    foreach($remove_from_array as $key => $value){
    if($value == NULL) {
    //$select .= ''.$value.',';
    }else{
    $select .= ''.$value.',';           
    }
    }

so now on call to unfollowAction the specified ID is removed from the attribute value but keeps any other ID values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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