【问题标题】:Way to access from a repository to two values in session - Symfony2从存储库访问会话中两个值的方法 - Symfony2
【发布时间】:2015-04-11 18:10:45
【问题描述】:

我在 Session 中有两个值:

课堂和科目

我在我的 web 中使用 Handlers 更新了这个值,并且我在多个控制器中使用,但是我有问题吗?

我调用一个 EntityRepository 并多次使用各种函数,具有相同的两个值:教室和主题,所以我决定修改 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');
    }
....

但是,显然,不起作用,因为我无法访问 EntityRepository 中的 Session,而且我认为将这 2 个值放在这个 ER 的每个函数中,这真的很糟糕,所以,有什么选择吗?

谢谢你

【问题讨论】:

    标签: php symfony session repository


    【解决方案1】:

    您是否考虑过 DI(推迟)会议?您可以将以下内容添加到您的服务中:

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

    然后,创建setSession 方法,将会话存储在变量中,您可以从$this->session 访问会话...

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2017-11-03
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2014-07-10
      相关资源
      最近更新 更多