Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the Symfony user session in the model (Doctrine)

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?

like image 682
Dandy Avatar asked Dec 22 '22 20:12

Dandy


2 Answers

This might be of interest:

http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/

like image 139
Tom Avatar answered Dec 28 '22 06:12

Tom


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.

like image 35
ncuesta Avatar answered Dec 28 '22 08:12

ncuesta