I want to create a "like/dislike" button in Ajax with Symfony 1.4.
I have these tables :
| Song | ------- n --------------------------- n ---------- | sfGuardUser |
|
| LikeSong | `
I've read symfony AJAX documentation but it is 1.0 documentation. 1.4 is very light. So, here is what I tried to do first.
In /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();
?>
In /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 }
In /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
}
}
}
When the user click, "I like this song" should be replaced by "I don't like this song".