Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2会话>存储和检索会话中项目的数组/集合_Php_Session_Symfony - Fatal编程技术网

Php Symfony2会话>存储和检索会话中项目的数组/集合

Php Symfony2会话>存储和检索会话中项目的数组/集合,php,session,symfony,Php,Session,Symfony,我不知道这是否值得质疑。我已经阅读了文档,但由于来自旧的PHP$\u会话环境,我不确定如何实现。我有一个测验类型的申请。在开始测验时,我想获取测验中的所有问题,并将它们存储在会话中。检索下一个问题时,我想检查会话中是否存在这些问题,如果存在,则检索下一个问题。我有以下资料: public function nextQuestionAction() { $data = $this->getRequest()->request->all(); $session =

我不知道这是否值得质疑。我已经阅读了文档,但由于来自旧的PHP$\u会话环境,我不确定如何实现。我有一个测验类型的申请。在开始测验时,我想获取测验中的所有问题,并将它们存储在会话中。检索下一个问题时,我想检查会话中是否存在这些问题,如果存在,则检索下一个问题。我有以下资料:

public function nextQuestionAction()
{
    $data = $this->getRequest()->request->all();

    $session = new Session();
    $session->start();

    if ($session->has('quiz_questions'))
    {
        $session->get('quiz_questions');
        // Get the next question from the session, 
        // based on the previous question's ID, $data['question_id'];
        // The questions are ordered by an order_index and not by id, but
        // I get the session index based on $data['question_id'] and then 
        // retrieve the next one in line based on the index, or something
        // along that line
        // If there is no next question, then the session must be unset
    }
    else
    {
        $session->create('quiz_questions');

        $quiz = $this->getDoctrine()
        ->getRepository('CriticalReadingQuizBundle:Quiz')
        ->find($data['quiz_id']);

        $questions = $quiz->getQuestions();
        // Store questions in session in such a way that I can retrieve 
        //the next question when this function gets called again.
        //The questions are ordered by an order_index and not by id, but they can
        //be stored in the session in their order with some arbitrary index so that
        //the next in line can be retrieved based on the index of the previous.
    }

    ... // Rest of code returns the fetched question to the front-end.
    // If this is the first time that the function is called, the first question is returned.
    //If there are no questions left (previous fetch fetched the last question), return null.
}

如果有人可以帮助我在Symfony2中使用会话的代码,因为这是我第一次使用它们。如果需要,我可以发布测验和问题实体,但我认为没有必要发布。

这里有一个基本的Symfony2会话对象示例:

感谢您的反馈。我已经阅读了这个文档。但是,这仅指定如何设置单个会话属性。我正在寻找一种解决方案,可以设置整个会话对象,甚至更广泛地说,可以在会话中设置对象的数组/集合。