使用循环将对象持久保存到数据库

时间:2012-12-11 05:00:52

标签: php symfony

我有一个表单,它返回多个值以及我想要存储在数据库中的每个值,

我为此创建了一个测试表单

<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>

我的提交答案行动目前是这样写的。

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);

当我执行var_dump时,所有内容似乎都正确关联,这意味着answers数组中填充了正确的数据,所有其他变量也是如此,我的问题实际上是将其持久保存到数据库中。当它运行时,它会返回此错误,在我看来它似乎不知道要放入行中的变量。

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"}:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read, date_created) VALUES (2, 1, 1, 'Answer 1', 0, '2012-12-11 15:12:51')' at line 1

非常感谢任何帮助,因为这是一个帮助我尝试更多地了解Web开发的个人项目。

2 个答案:

答案 0 :(得分:4)

不确定是什么问题。一些事情:

在循环之前:

$database = $this->getDoctrine()->getManager();

它应该是$ em或$ entityManager(因为这就是它的名称......)

应在循环后调用:

$database->flush();

您无需使用Doctrine2处理数据库中保存的ID。所以不要这样做:

$studentAnswer->setStudentID($studentID);

请改为:

$studentAnswer->setStudent($student);

答案 1 :(得分:1)

哇好吧,所以这个问题根本与Symfony或Doctrine无关,

问题基本上是read字段,我相信它是SQL中的关键字,因此它将其视为关键字而不是列字段名称。我不得不将其重命名为is_read