捕获的PHP异常与doctrine2插入日期
问题描述:
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(),
));
}
}
我有这样的控制器,可产生上提交 INSERT INTO ads_list(标题,说明,posted_at)VALUES(?,?,?)”使用参数[ “r3123r2” 下面的SQL ,“3er233r123r2”,“2017-10-20”]捕获的PHP异常与doctrine2插入日期
但是MySQL说不! :) 的东西是错误的(这个查询看起来不错,对我来说)
posted_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
有什么想法?
答
你正在使用的列名的desc
关键字是在MySQL中的保留字,学说不会自动引用标识符:
http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#quoting-reserved-words
有时需要引用列或表名由于保留字冲突。学说不会自动引用标识符,因为它会导致比解决更多的问题。引用表格和列名需要使用定义中的记号明确进行。
+0
该死的:)。谢谢:D –
你在'parameters.yml'文件中设置了'database_name'吗? –
是的,我没有:)我已经有一些工作的SQL :) –
如果你想使用保留字,你可以像这样将它添加到这个 ,它不会给出任何错误。 –