Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 Sessions > Store & Retrieve array/collection of items in session
    primarykey
    data
    text
    <p>I do not know if this is question worthy. I have read up on the documentation, but coming from and old PHP $_SESSION environment I am not sure how to implement. I have a quiz type application. At initiation of the quiz I want to get all the questions in the quiz and store them in the session. When retrieving the next question I want to check if the questions exist in the the session, if they do then I retrieve the next question. I have the following:</p> <pre><code>public function nextQuestionAction() { $data = $this-&gt;getRequest()-&gt;request-&gt;all(); $session = new Session(); $session-&gt;start(); if ($session-&gt;has('quiz_questions')) { $session-&gt;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-&gt;create('quiz_questions'); $quiz = $this-&gt;getDoctrine() -&gt;getRepository('CriticalReadingQuizBundle:Quiz') -&gt;find($data['quiz_id']); $questions = $quiz-&gt;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. } </code></pre> <p>If somebody could please assist me with the code on working with sessions in Symfony2 as it is the first time that I am working with them. I can post the Quiz and Question entities if they are required, but I do not think that is necessary.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload