The following snippets provide access to the Symfony User object from various parts of a Symfony project.
Template / View
1 | $user = $sf_user; |
Model or Form
1 | $user = sfContext::getInstance()->getUser(); |
Note: the context isn’t setup when using the command line, therefore if you use this in the save() method of an object and try to populate the object from a data-load it will throw an error: ‘The “default” context does not exist’.
Action
1 | $user = $this->getUser(); |
or
1 | $user = sfContext::getInstance()->getUser(); |
sfGuardUser and sfGuardUserProfile
sfGuard is a popular plugin for Symfony which provides a complete set of tools for managing users, groups, permissions and profiles. Once the user object has been retrieved as above, the sfGuard user and profile are accessed as follows:
Template / View
1 | $profile = $user->getGuardUser()->getProfile(); |
A Note on Snippets: When using frameworks such as Symfony it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.


27th Apr
Hugo says:
Retrieving a factory object (request, response, user, cache…) from the sfContext singleton is the ugly way as it relies on a string couple between classes. The ideal way to use the context in a model is to pass it explicitly with a setter like below :
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class MyModelObject
{
private $user = null;
public function setUser(sfUser $user)
{
$this->user = $user;
}
public function save($conn = null)
{
$this->setUserId($this->user->getId());
return parent::save($conn);
}
}
And in the action :
2
3
4
5
6
7
{
$obj = new MyModelObject();
$obj->setUser($this->getUser());
// You can access the context with $this->getContext();
}
++
Hugo.
12th May
ErisDS says:
@hugo: Sorry for the delay in my reply, I’m currently on holiday :) Thanks for your insights on this problem & taking the time to share them here. I plan update this post with your solution when I get home & have some time to experiment with using this method for forms :)
28th Jul
Jimmy says:
Do you also now how to get the userID from a view, model, action etc?
I tried sfContext::getInstance()->getUserId(); and sfContext::getInstance()->getUser()->getId();
..?
Cheers, Jim
28th Jul
ErisDS says:
Hi Jimmy,
sfContext::getInstance()->getUser()->getId(); should work anywhere but the view, but is the ugly way to do it as Hugo explains above.
In an action you can use $this->getUser()->getId(); and in a view it will be $sf_user->getId();
If you can’t get it to work then get in touch with me via my contact form and we’ll take a closer look.
9th Aug
pfwd says:
Hi Jimmy,
I couldn’t get the following to work in my action:
Even tho this worked:
I did manage to get all the fields (including the id ) by adding getGuardUser() like so:
This is a link to my source http://forum.symfony-project.org/index.php/m/75314/
Hope that helps
16th Oct
Arif says:
Thanks, it’s really very easy to understand , keep going on
3rd Nov
symfonybr :: Symfony - Snippet - Maneiras de acessar o objeto User pinged back:
[...] font: http://erisds.co.uk/symfony/snippet-symfony-user-access-the-user-object [...]
29th Apr
Closer To The Ideal » Blog Archive » In Symfony, context objects should be given to models via setters pinged back:
[...] guess I knew this, but it is good to be reminded. Check out the comment by Hugo: Retrieving a factory object (request, response, user, cache…) from the sfContext singleton is [...]