Reputation: 5
I'm currently creating an application in the yii-framework. One of the purposes of the application is to be able to rate games at different predefined categories and platforms. A game can belong to multiple platforms.
An example of how this would be: I choose to rate a game called "x". I can see that the game is published on 4 different platforms, so when i rate the game i choose the platform i played it on. thereafter i rate the game on the 5 different categories. If I return to this game later on, i should be able to update that rating, but i should also be able to create a new rating for a different platform.
It's especially the last part i'm unsure of. My guess on how to make this would be to create a dropdownlist with the platforms the game belongs to, and have some AJAX function decide wether it should create a new rating or update an existing one. however, I have no clue on how to do that since I've rarely done something with AJAX.
Can you please give me some hints on how to achieve this?
EDIT: It might look like i haven't tried something, so I will post some of the code i've created so far (for some reason I named it ranking instead of rating...). As you can see it's without any AJAX and it does not take in to consideration the platform:
protected function createRanking($model)
{
$user_id=Yii::app()->user->getId();
$game_id=$model->id;
$rank=ranking::model()->find("create_user_id=$user_id and game_id=$game_id");
if($rank===null){
$ranking=new Ranking;
}
else{
$ranking=$rank;
}
if(isset($_POST['Ranking']))
{
$ranking->game_id=$model->id;
$ranking->attributes=$_POST['Ranking'];
$valid = $ranking->validate();
if ($valid)
{
$ranking->save(false);
$this->redirect(array('index'));
}
}
return $ranking;
}
Upvotes: 0
Views: 566
Reputation: 688
http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii/ http://api.jquery.com/jQuery.ajax/
I think you should read those first (and have some PHP and javascript knowledge).
If you want to use DropDowns you should look at EventHandlers (jQuery helps you with that). Via the selector of the dropdown you can attach such an handler. I will not add any more to this, with the keywords I gave you, you should find excellent documentation on that topic.
On the side of Yii you handle an AjaxRequest just like you handle a normale HTTP-Request. So you would have an action in which you have control-flow which checks for the existence of the record (findByPk would be an option).
It seems to me, that you have very basic questions and giving you code would only make things easy for you in the short run. I really recommend working through the tutorials on yii-framework: http://www.yiiframework.com/doc/guide/ http://www.yiiframework.com/doc/blog/
Edit: Your question still isn't very clear imo (which kind of hints towards a lack of reading about the topic). As you have already figured out "isNewRecord" is the way to go, for checking if it is an update or a create. If there is some problem related to it, you should consider posting code containing it and reworking the question.
Anyway, here is some minor non-question related advice to your code:
$ranking = ranking::model()->findByAttributes(array(
"create_user_id" => $user_id,
"game_id" => $game_id
));
if($ranking===null) {
$ranking=new Ranking;
}
The ranking/rank variable declaration is unclear since you are using different names for the same thing. Also as the code above shows it is not needed. Also you basically used SQL in your find method which is considered somewhat of a bad practice since the whole idea of Yii is only speaking in DB language when there is absolutely no way around it. Also the param-binding way with the array is more safe (which in this case is not much of a concern I think).
Upvotes: 1