i just give my thought ,but never do it reality :
there are there concepts involved , user some_topic and follow(it is a relation);
user can follow every thing such as user , topic , post ,image etc… this means if the followed target changed you should notify the follower .
i may design a follow table like this : follow(id, user_id, model_class, model_id )
if click the follow button use ajax to insert a record , switch "follow" to "unfolow" when ajax success
return back , using js do that is easy !
you generate a Follow class using gii/giix…
$follow = new Follow();
$follow->user_id = Yii::app()->user->id;
$follow->model_class = 'the class of target model'; //you post it with ajax $_POST['ModelClass']
$follow->model_id = $_POST['model_id'];
$follow->save();//then save it ,may be you should check if the current user had follow the target already
next time if you load the model ,you should check whether current user has followed it :
in model class :
public function hasFollowed($userId){
$modelClass = get_class($this);
$modelId = $this->id;
return Follow::model()->exists('user_id=:uid AND model_class=:modelClass AND model_id=:modelId', array (':uid'=>$userId,':modelClass'=>$modelClass,':modelId'=>$modelId ));
}
thus in you view.php file , you can check if current user has followed you model ;
<?php $followed = $model->hasFollowed(Yii::app()->user->id); ?>
// you follow button can use this value to decide which class to apply :
<?php
if($followed ){
render unfollow logic
}else{
render follow logic here
}
?>
the unfollow is the same logic just delete a record from you follow table.
the best choice is to write a behavior ,then you can config it to any model you want them have this functionality :
class FollowBehavior extends CActiveRecordBehavior{
public function hasFollowed($userId){
$modelClass = get_class($this->getOwner());
$modelId = $this->getOwner()->id;
return Follow::model()->exists('user_id=:uid AND model_class=:modelClass AND model_id=:modelId', array (':uid'=>$userId,':modelClass'=>$modelClass,':modelId'=>$modelId ));
}
}
//you should implements the unfollow function too , say in some controller :
public function actionUnFollow(){
if(Yii::app()->request->getIsAjaxRequest() && Yii::app()->request->getIsPostRequest){
//do unfollow here $_POST['model_class'] ,$_POST['model_id'], Yii::app()->user->id;
above there variables you can use! accomplish it yourself <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/biggrin.gif' class='bbc_emoticon' alt=':D' />
}
}
there are some additional things you should complete , this is just an start point