2017-10-19 30 views
0
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 

有什麼想法?

+0

你在'parameters.yml'文件中設置了'database_name'嗎? –

+0

是的,我沒有:)我已經有一些工作的SQL :) –

+0

如果你想使用保留字,你可以像這樣將它添加到這個 ,它不會給出任何錯誤。 –

回答