I have a magento custom collection and each item in the collection has its own landing page (collection/view/index/id/12 etc) in the frontend & managed in the admin backend.
I have a controller action that allows users to "follow" each item, with the users ID saved/added to the items field value.
Example of the value of the Field/attribute below
//Follow Action Working..
//Users ID added to field when followAction accessed via a follow link.
$model2 = Mage::getModel("userprofiles/userprofiles")->load($id);
$FollowProfiles = $model2->getFollowProfiles();
$model->setFollowProfiles(''.$FollowProfiles.''.$myprofileid.',');
$model->save();
Mage::getSingleton('core/session')->addSuccess('Sucessfully followed.');
$this->_redirectReferer();
//saves as
123,321,220,125,
The follow action works as intended. However trying to get the unfollow action is not wanting to work. Code below.
//Get field/attribute values ie 123,234,345,456,
$FollowProfilesArray = array($model2->getFollowProfiles());
//$profileid will be current users id
//used to remove user id from array
$remove_from_array = array_diff($FollowProfilesArray,array($profile_id,));
foreach($remove_from_array as $key => $value){
$select .= ''.$value.',';
}
//saves all ids except the removed users id
$model->setFollowProfiles($select);
Basically for some reason when using array($model2->getFollowProfiles()) does not allow the removal of the user id from the field value saves it as 123,234,345,456,,
but..
when i set the array as a hardcoded value array(123,234,345,456,) it works and removes the id specified.
Any reason why array($model2->getFollowProfiles()) does not work as it equals 123,234,345,456,
Do i have to implode, explode the $model2->getFollowProfiles() or something..??
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.