简体   繁体   中英

Way to access from a repository to two values in session - Symfony2

i has in the Session two values:

classroom and subject

I update this values with Handlers in my web, and i use in multiple controllers, but i have a problem?

I call one EntityRepository and use a various functions multiple times, with the same two values: classroom and subject, so I decided to modify the constructor of the EntityRepository:

class StudentRepository extends EntityRepository
{
    function __construct($em, ClassMetadata $class) {

      parent::__construct($em, $class);
      $this->classroom = $this->getRequest()->getSession()->get('classroom');
      $this->subject = $this->getRequest()->getSession()->get('subject');
    }
....

But, obvius, not work because i cant access to the Session in a EntityRepository, and i think put this 2 values in every function of this ER its really a shit, so, Any option?

Thanks you

Have you considered DI (deferred) the session? You can add the following to your service:

student_repository_Service:
...
calls:
    - [setSession, ["@session"]]

Then, creating the setSession method, storing the session in a variable, and you could access the session from $this->session ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM