How should one access Symfony's current user session in Doctrine's model?
The two ways (that I know of) to do it are to either pull it from the sfContext inside the model:
sfContext::getInstance()->getUser()->getCanSwim();
Or, pass the sfUser instance (or piece) directly to the model from the controller:
UserTable::goSwimming($sf_user->can_swim);
But, are any of these methods better than the other, or is this not the correct way to do it all?
This might be of interest:
http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/
For this kind of situations, you should consider using the Dependency Injection design pattern as Fabien Potencier clearly explains.
The idea is that you have to inject into your dependant object all of its dependencies such as -in your case- the user or the context.
That (the second option) would be the less MVC-killing way, afaic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With