带有doctrine2插入日期的未捕获的PHP异常

时间:2017-10-19 15:26:08

标签: php mysql symfony doctrine-orm

class PostController extends Controller
{
/**
 * @param Request $request
 * @Route("/post", name="post")
 */
public function newAction(Request $request)
{
    // create a Post and give it some dummy data for this example
    $task = new AdsList();
    $task->setTitle('Write a blog post');
    $task->setpostedAt(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('title', TextType::class)
        ->add('desc',TextType::class)
        ->add('postedAt', DateType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Post'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // $form->getData() holds the submitted values
        $task = $form->getData();

        // save the task to the database
        $em = $this->getDoctrine()->getManager();
        $em->persist($task);
        $em->flush();

        return $this->redirectToRoute('list');
    }


    return $this->render('postlist/post.html.twig', array(
        'form' => $form->createView(),
    ));
}

}

我有这个Controller在提交时生成以下SQL INSERT INTO ads_list(title,desc,posted_at)VALUES(?,?,?)'与params [" r3123r2"," 3er233r123r2"," 2017-10-20"]

然而MySQL说没有! :) 出了点问题(这个查询对我来说很好)

published_at看起来像

     /**
     * @ORM\column(type="date", name="posted_at", options={"default": 0})
     */
     protected $postedAt;

我收到以下错误

    Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing 'INSERT INTO ads_list (title, desc, posted_at) VALUES (?, ?, ?)' with params ["r3123r2", "3er233r123r2", "2017-10-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 'desc, posted_at) VALUES ('r3123r2', '3er233r123r2', '2017-10-20')' at line 1" at /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 94

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您用作列名的desc关键字是MySQL中的保留字,Doctrine不会自动引用标识符:

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html#quoting-reserved-words

  

由于保留字冲突,有时需要引用列或表名。 Doctrine不会自动引用标识符,因为它会导致比解决方案更多的问题。引用表和列名称需要使用定义中的刻度明确地完成。