1

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

4
  • 3
    some more information would be welcome? What kind of "session" variable do you need in your model? Where is it created/assigned, where is it needed, etc. Because, usually it's a sign of a bad model when you need sfContext::getInstance() in your model, just like you said. Commented Dec 1, 2011 at 12:14
  • 1
    what do you want to do? If you want to keep some values during a session you can add them as attributes to the user, for example Commented Dec 1, 2011 at 14:33
  • Are you looking for a backend application created with the admin generator? Commented Dec 1, 2011 at 20:51
  • Hi Grad... as mentioned am using setAttribute for userid in lib class...i need to access the session in model class within save method ...but i kno sfContext::getInstance() is bad idea so am looking for alternate... Commented Dec 2, 2011 at 13:04

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

6 Comments

Hi maerlyn ...as mentioned am using setAttribute for userid in lib class...i need to access the session in model class within save method ...but i kno sfContext::getInstance() is bad idea so am looking for alternate..
I understand that, and gave you an alternative. But without telling why you need the id, I cannot give a better option.
@Maerlyn: I tried that in same way. But I unable to get user id in model.$this->_user this returns empty on save function. help me out.
Are you sure you're saving the record you set the user on?
@Maerlyn: Basically i am saving contest with current logged in user id here lib/model/Contest.class.php. I am using sfdoctrineguardplugin for authentication. How can I do this.
|
-1

Session variables should be stored as user's attributes.

// in an action: 
$this->getUser()->setAttribute('current_order_id', $order_id);

See how to get it back.

// later on, in another action, you can get it as:
$order_id = $this->getUser()->getAttribute('current_order_id', false);
if($order_id!==false)
{
    // save to DB
} else {
    $this->getUser()->setFlash('error', 'Please selected an order before you can do stuff.');
    // redirect and warn the user to selected an order
    $this->redirect('orders');
}

3 Comments

This is in no way an answer to the question.
Pibeiro am not looking for the answer u gave...am asking for getting session in model class within save method without using sfContext::getInstance()...
But from where are you saving the model? You gotta save it from an action right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.