Symfony 1.4 で Ajax に「好き/嫌い」ボタンを作りたいです。
私はこれらのテーブルを持っています:
| Song | ------- n --------------------------- n ---------- | sfGuardUser |
|
| LikeSong | `
symfony AJAX のドキュメントを読みましたが、1.0 のドキュメントです。1.4はとても軽いです。ということで、まずやってみたのがこちら。
で/app/frontend/module/likesong/_voting.php
:
<?php
if($song->hasVote())
{
jq_link_to_remote('I do not like', array('complete' => '[??? Update my link]', 'url' => 'likesong/notlike?id_song='.$song->getId()));
}
else
{
jq_link_to_remote('I like', array('complete' => '[??? Update my link]', 'url' => 'likesong/like?id_song='.$song->getId()));
}
echo ' - '.$song->getNbVote();
?>
で/app/frontend/config/routing.yml
:
song_like:
url: /song-like/:id
param: { module: song, action: like }
song_notlike:
url: /song-not-like/:id
param: { module: song, action: notLike }
の/app/frontend/module/likesong/actions.class.php
public function executeLike(sfWebRequest $request)
{
if ($request->isXmlHttpRequest())
{
if(USER HAS NOT YET VOTED)
{
$this->vote = new LikeSong();
$this->vote->setSongId($this->song()->getId());
$this->vote->setSfGuardUserId($this->getUser()->getId());
$this->vote->save();
return $this->renderText('notlike');
else
{
// Display flash
}
}
}
public function executeNotLike(sfWebRequest $request)
{
if ($request->isXmlHttpRequest())
{
if(USER ALREADY VOTED)
{
// Delete form database
return $this->renderText('like');
else
{
// Display flash
}
}
}
ユーザーがクリックすると、「I like this song」が「I don't like this song」に置き換えられます。