Search code examples
symfony1session-variables

How to get Symfony session variable in model?


How can I pass session variable in symfony model without using sfContext::getInstance()?


Solution

  • The recommended way is called dependency injection, and works like this: you create a setUser() method in your model file, that saves the given parameter to a private property:

    class Foo {
      private $_user;
    
      public function setUser(myUser $user) {
        $this->_user = $user;
      }
    
      // ... later:
    
      public function save(Doctrine_Connection $conn = null) {
        // use $this->_user to whatever you need
      }
    }
    

    This looks clumsy, because it is. But without you answering the question what are you trying to do? I cannot give an alternative.

    Recommended articles: