Henry Boisgibault
Henry Boisgibault

Reputation: 826

Symfony2 - app.user not synced with database

I have a page called lobby in which a user can accept a friend request. Accepting a friend request leads to this action :

public function acceptFriendRequestAction($requestId)
{
    $user = $this->getUser();

    // Here $user is modified and changes are saved in database

    return $this->redirect('ACAppBundle:Lobby:index');
}

A template is rendered, using app.user to show friends and requests. However, changes in the database are not taken into account. User object is the same as it was before acceptFriendRequestAction. When page is refreshed, app.user is synced with database.

Why do I need to refresh the page to see changes in the database ? How to set app.user as updated user ?

When I use forward instead of redirect it works but I don't want to use this because forward doesn't change the URL.

I also noticed that sometimes a class named Proxies/.../User is used instead of User. Could that have something to do with my problem ? Thank you for helping, I've been stuck on this for days...

Upvotes: 2

Views: 125

Answers (3)

Henry Boisgibault
Henry Boisgibault

Reputation: 826

So it seems that i've found the solution :

I replaced :

return $this->redirect('ACAppBundle:Lobby:index');

with

return $this->redirect($this->generateUrl('ac_app_lobby'));

Now after redirection, new friend is shown without needing to reload page.

I don't understand what's the difference between the two lines though. Can someone explain that?

Upvotes: 1

qooplmao
qooplmao

Reputation: 17759

You aren't updating the actual relationship when you are removing the friendship request. When you do the removeElement you are just removing it in memory until you set the sender or receiver to null.

You can do this by hand like..

$user->removePendingRequest($request);
$request->setSender(null);
// or $request->setReceiver(null);

Or you can add it to the add/remove to do it automatically like..

public function removeFriendship(FriendshipInterface $friendship)
{
    if ($this->friendships->contains($friendships)) {
        $this->friendships->removeElement($friendships);
        $friendship->setSender(null);
        // or $friendship->setReceiver(null);
    }
}

Upvotes: 0

Alexandr Lensky
Alexandr Lensky

Reputation: 233

You need add cascade options for your relations in Friendship class for $request field

More info http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

Upvotes: 1

Related Questions