Musings of ErisDS
beta
ErisDS

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.

Related Posts

Share this...

  •  Add 'Snippet: Symfony User - Access the User Object' to Del.icio.us
  • Add 'Snippet: Symfony User - Access the User Object' to Twitter
  • Add 'Snippet: Symfony User - Access the User Object' to digg
  • Add 'Snippet: Symfony User - Access the User Object' to FURL
  • Add 'Snippet: Symfony User - Access the User Object' to reddit
  • Add 'Snippet: Symfony User - Access the User Object' to Technorati
  • Add 'Snippet: Symfony User - Access the User Object' to Newsvine
  • Add 'Snippet: Symfony User - Access the User Object' to Stumble Upon
  • Add 'Snippet: Symfony User - Access the User Object' to Google Bookmarks
  • Add 'Snippet: Symfony User - Access the User Object' to FaceBook

Comments

12 Comments to "Snippet: Symfony User – Access the User Object"
  1. 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 :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?php

    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 :

    1
    2
    3
    4
    5
    6
    7
    public function executeAction(sfWebRequest $request)
    {
      $obj = new MyModelObject();
      $obj->setUser($this->getUser());

      // You can access the context with $this->getContext();
    }

    ++

    Hugo.

  2. 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 :)

  3. 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

  4. 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.

  5. 9th Aug

    pfwd says:

    Hi Jimmy,
    I couldn’t get the following to work in my action:

    1
    $this->getUser->getUserId();

    Even tho this worked:

    1
    $this->getUser->getUsername();

    I did manage to get all the fields (including the id ) by adding getGuardUser() like so:

    1
    $this->getUser()->getGuardUser()->getId();
    1
    $this->getUser()->getGuardUser()->getUsername();
    1
    $this->getUser()->getGuardUser()->getCreatedAt();

    This is a link to my source http://forum.symfony-project.org/index.php/m/75314/
    Hope that helps

  6. 16th Oct

    Arif says:

    Thanks, it’s really very easy to understand , keep going on

  7. symfonybr :: Symfony - Snippet - Maneiras de acessar o objeto User

    3rd Nov

  8. Closer To The Ideal » Blog Archive » In Symfony, context objects should be given to models via setters

    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 [...]

  9. 3rd Jan

    Carlos says:

    Thank you very much! Very useful information!

  10. 18th Mar

    valentino says:

    i love you so much! been looking around for so long just for a page like this. Thanks also to pfwd: you saved me a sooo boring googling =)

  11. 24th Mar

    ErisDS says:

    It’s the simplest things that are often the hardest to find/figure out!

  12. 17th Jul

    Verine says:

    Thanks a lot. We can’t underestimate the effect that tweaking MySQL variables can have on response times.

    Yours, Verine.

Add your thoughts

  • XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>