Persisting objects to a database using a loop

Posted by ChaoticLoki on Stack Overflow See other posts from Stack Overflow or by ChaoticLoki
Published on 2012-12-11T05:00:51Z Indexed on 2012/12/11 5:03 UTC
Read the original article Hit count: 117

Filed under:
|

I have a form that returns multiple values and each value I would like to store in a database,

I created a test form for this purpose

<form method="post" action="{{ path('submit_exercise', {'page': 1}) }}">
    <input type="hidden" name="answers[]" value="Answer 1" "/>
    <input type="hidden" name="answers[]" value="Answer 2" "/>
    <input type="hidden" name="answers[]" value="Answer 3" "/>
    <input type="hidden" name="answers[]" value="Answer 4" "/>
    <input type="hidden" name="answers[]" value="Answer 5" "/>
    <input type="hidden" name="answers[]" value="Answer 6" "/>
    <input type="hidden" name="answers[]" value="Answer 7" "/>
    <input type="submit" name="submit" />
</form>
</body>
</html>

My submit answers Action is currently written like so.

public function submitAnswersAction($page)
{
    //get submitted data
    $data = $this->get('request')->request->all();
    $answers = $data['answers'];

    //get student ID
    $user = $this->get('security.context')->getToken()->getUser();
    $studentID = $user->getId();

    //Get Current time
    $currentTime = new \DateTime(date("Y-m-d H:m:s"));


    //var_dump($answers);
    //var_dump($studentID);
    //var_dump($currentTime);

    for($index = 0; $index < count($answers); $index++)
    {
        /*echo "Question ". ($index + 1) ."<br />";
        echo "Student ID: ". $studentID."<br />";
        echo "Page Number: $page <br />";
        echo "Answer: $answers[$index]"."<br />";
        echo "<br />";*/

        $studentAnswer = new StudentAnswer();
        $studentAnswer->setStudentID($studentID);
        $studentAnswer->setPageID($page);
        $studentAnswer->setQuestionID($index+1);
        $studentAnswer->setAnswer($answers[$index]);
        $studentAnswer->setDateCreated($currentTime);
        $studentAnswer->setReadStatus(0);

        $database = $this->getDoctrine()->getManager();
        $database->persist($studentAnswer);
        $database->flush();

    }

    return new Response('Answers saved for Student: '.$user->getFullName().' for page: '.$page);

When I do a var_dump everything seems to be associated correctly, meaning that the answers array is populated with the right data and so is every other variable, my problem is actually persisting it to the database. when run it returns with this error which seems to me like it doesn't know what variables to put into the row.

An exception occurred while executing 'INSERT INTO Student_Answer (student_id, page_id, question_id, answer, read, date_created) VALUES (?, ?, ?, ?, ?, ?)' with params {"1":2,"2":"1","3":1,"4":"Answer 1","5":0,"6":"2012-12-11 12:12:20"}:

Any help would be greatly appreciated as this is a personal project to help me try and understand web development a bit more.

© Stack Overflow or respective owner

Related posts about php

Related posts about symfony2