我有一个我不理解的错误:
\n\n\n\n\n执行 \'INSERT INTO achats (year_month,\n modele, code_reference, date_de_ajout, cout, date_de_achat,\nimportance, ordre, type, location_de_achat) VALUES (?, ?, ?, ?, ?, ?,\ 时发生异常n ?, ?, ?, ?)\' ,参数为 ["2020-07", "St\\u2011Luc\n (echo\\u2011graphie)", null, "2020-01-23", 2.97, "2020- 01-23", 空,\n 13, 24, 7]:
\n\nSQLSTATE[42000]: 语法错误或访问冲突: 1064 您的 SQL 语法中有\n 错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 \'year_month、\n modele、code_reference、date_de_ajout、cout、date_de_achat、importan\'\n 附近使用的正确语法
\n
我的实体是:
\n\n<?php\n\nnamespace App\\Entity;\n\nuse Doctrine\\ORM\\Mapping as ORM;\n\nuse Symfony\\Component\\Validator\\Constraints as Assert;\n\n/**\n * Achats\n *\n * @ORM\\Table(name="achats", indexes={@ORM\\Index(name="type", columns={"type", "location_de_achat"}), @ORM\\Index(name="location_de_achat", columns={"location_de_achat"}), @ORM\\Index(name="Modele", columns={"Modele"}), @ORM\\Index(name="IDX_9920924E8CDE5729", columns={"type"})})\n * @ORM\\Entity\n */\nclass Achats\n{\n /**\n * @var int\n *\n * @ORM\\Column(name="id", type="bigint", nullable=false, options={"unsigned"=true})\n * @ORM\\Id\n * @ORM\\GeneratedValue(strategy="IDENTITY")\n */\n private $id;\n\n /**\n * @return int\n */\n public function getId()\n {\n return $this->id;\n }\n\n /**\n * @param int $id\n * @return Achats\n */\n public function setId($id)\n {\n $this->id = $id;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @ORM\\Column(name="year_month", type="string", length=255)\n * @Assert\\Length(\n * min = 7,\n * max = 7,\n * minMessage = "Your date name must be at least {{ limit }} characters long",\n * maxMessage = "Your date name cannot be longer than {{ limit }} characters"\n * )\n */\n private $yearMonth;\n\n public function getYearMonth(): ?string\n {\n return $this->yearMonth;\n }\n\n public function setYearMonth(string $yearMonth): self\n {\n $this->yearMonth = $yearMonth;\n\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var string|null\n *\n * @ORM\\Column(name="modele", type="string", length=255, nullable=true)\n */\n private $modele;\n\n /**\n * @return string|null\n */\n public function getModele()\n {\n return $this->modele;\n }\n\n /**\n * @param string|null $modele\n * @return Achats\n */\n public function setModele($modele)\n {\n $this->modele = $modele;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var string|null\n *\n * @ORM\\Column(name="code_reference", type="string", length=255, nullable=true)\n */\n private $codeReference;\n\n /**\n * @return string|null\n */\n public function getCodeReference()\n {\n return $this->codeReference;\n }\n\n /**\n * @param string|null $codeReference\n * @return Achats\n */\n public function setCodeReference($codeReference)\n {\n $this->codeReference = $codeReference;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var \\DateTime|null\n *\n * @ORM\\Column(name="date_de_ajout", type="date", nullable=true)\n */\n private $dateDeAjout;\n\n /**\n * @return \\DateTime|null\n */\n public function getDateDeAjout()\n {\n return $this->dateDeAjout;\n }\n\n /**\n * @param \\DateTime|null $dateDeAjout\n * @return Achats\n */\n public function setDateDeAjout($dateDeAjout)\n {\n $this->dateDeAjout = $dateDeAjout;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var int|null\n *\n * @ORM\\Column(name="cout", type="float", nullable=true, options={"unsigned"=true})\n */\n private $cout;\n\n /**\n * @return int|null\n */\n public function getCout()\n {\n return $this->cout;\n }\n\n /**\n * @param int|null $cout\n * @return Achats\n */\n public function setCout($cout)\n {\n $this->cout = $cout;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var \\DateTime|null\n *\n * @ORM\\Column(name="date_de_achat", type="date", nullable=true)\n */\n private $dateDeAchat;\n\n /**\n * @return \\DateTime|null\n */\n public function getDateDeAchat()\n {\n return $this->dateDeAchat;\n }\n\n /**\n * @param \\DateTime|null $dateDeAchat\n * @return Achats\n */\n public function setDateDeAchat($dateDeAchat)\n {\n $this->dateDeAchat = $dateDeAchat;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var \\Types\n *\n * @ORM\\ManyToOne(targetEntity="Types")\n * @ORM\\JoinColumns({\n * @ORM\\JoinColumn(name="type", referencedColumnName="id")\n * })\n */\n private $type;\n\n /**\n * @return \\Types\n */\n public function getType()\n {\n return $this->type;\n }\n\n /**\n * @param \\Types $type\n * @return Achats\n */\n public function setType($type)\n {\n $this->type = $type;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @var \\Lieux\n *\n * @ORM\\ManyToOne(targetEntity="Lieux")\n * @ORM\\JoinColumns({\n * @ORM\\JoinColumn(name="location_de_achat", referencedColumnName="id")\n * })\n */\n private $locationDeAchat;\n\n /**\n * @return \\Lieux\n */\n public function getLocationDeAchat()\n {\n return $this->locationDeAchat;\n }\n\n /**\n * @param \\Lieux $locationDeAchat\n * @return Achats\n */\n public function setLocationDeAchat($locationDeAchat)\n {\n $this->locationDeAchat = $locationDeAchat;\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @ORM\\Column(type="smallint", nullable=true)\n */\n private $importance;\n\n public function getImportance(): ?int\n {\n return $this->importance;\n }\n\n public function setImportance(?int $importance): self\n {\n $this->importance = $importance;\n\n return $this;\n }\n\n //=======================================================================================\n\n /**\n * @ORM\\Column(type="integer", nullable=true)\n */\n private $ordre;\n\n public function getOrdre(): ?int\n {\n return $this->ordre;\n }\n\n public function setOrdre(?int $ordre): self\n {\n $this->ordre = $ordre;\n\n return $this;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n控制器 :
\n\n<?php\n\nnamespace App\\Controller;\n\nuse App\\Entity\\Achats;\nuse App\\Form\\AchatsType;\nuse App\\Form\\AchatsNewType;\nuse Exception;\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Routing\\Annotation\\Route;\n\n/**\n * @Route("/")\n */\nclass AchatsController extends AbstractController\n{\n\n /**\n * @Route("/", name="achats_index", methods={"GET"})\n */\n public function index(): Response\n {\n $achats = $this->getDoctrine()\n ->getRepository(Achats::class)\n ->findAll();\n\n return $this->render(\'achats/index.html.twig\', [\n \'achats\' => $achats,\n ]);\n }\n\n /**\n * @Route("/achats/new", name="achats_new", methods={"GET","POST"})\n * @param Request $request\n * @return Response\n * @throws Exception\n */\n public function new(Request $request): Response\n {\n $achat = new Achats();\n $form = $this->createForm(AchatsNewType::class, $achat);\n $form->handleRequest($request);\n\n if ($form->isSubmitted() && $form->isValid()) {\n\n $now = new \\DateTime(\'now\');\n\n $entityManager = $this->getDoctrine()->getManager();\n $entityManager->persist($achat);\n $entityManager->flush();\n\n return $this->redirectToRoute(\'achats_index\');\n }\n\n return $this->render(\'achats/new.html.twig\', [\n \'achat\' => $achat,\n \'form\' => $form->createView(),\n ]);\n }\n\n /**\n * @Route("/achats/{id}", name="achats_show", methods={"GET"})\n */\n public function show(Achats $achat): Response\n {\n return $this->render(\'achats/show.html.twig\', [\n \'achat\' => $achat,\n ]);\n }\n\n /**\n * @Route("/achats/{id}/edit", name="achats_edit", methods={"GET","POST"})\n */\n public function edit(Request $request, Achats $achat): Response\n {\n $form = $this->createForm(AchatsType::class, $achat);\n $form->handleRequest($request);\n\n if ($form->isSubmitted() && $form->isValid()) {\n $this->getDoctrine()->getManager()->flush();\n\n return $this->redirectToRoute(\'achats_index\');\n }\n\n return $this->render(\'achats/edit.html.twig\', [\n \'achat\' => $achat,\n \'form\' => $form->createView(),\n ]);\n }\n\n /**\n * @Route("/achats/{id}", name="achats_delete", methods={"DELETE"})\n */\n public function delete(Request $request, Achats $achat): Response\n {\n if ($this->isCsrfTokenValid(\'delete\'.$achat->getId(), $request->request->get(\'_token\'))) {\n $entityManager = $this->getDoctrine()->getManager();\n $entityManager->remove($achat);\n $entityManager->flush();\n }\n\n return $this->redirectToRoute(\'achats_index\');\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n形式 :
\n\n<?php\n\nnamespace App\\Form;\n\nuse App\\Entity\\Achats;\n\nuse Symfony\\Component\\Form\\AbstractType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\DateType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\IntegerType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\MoneyType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\NumberType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType;\nuse Symfony\\Component\\Form\\FormBuilderInterface;\nuse Symfony\\Component\\OptionsResolver\\OptionsResolver;\n\nclass AchatsNewType extends AbstractType\n{\n public function buildForm(FormBuilderInterface $builder, array $options)\n {\n $builder\n ->add(\'importance\', IntegerType::class, [\n\n \'required\' => false,\n\n \'attr\' => [\n \'placeholder\' => \'Importance\',\n \'min\' => \'0\',\n \'max\' => \'100\',\n \'step\' => \'0.01\'\n ]\n ])\n\n ->add(\'yearMonth\', TextType::class, [\n\n \'attr\' => [\n \'placeholder\' => \'YYYY\xe2\x80\x91MM\',\n \'min\' => \'7\',\n \'max\' => \'7\',\n ]\n ])\n\n ->add(\'ordre\', IntegerType::class, [\n\n \'required\' => false,\n\n \'attr\' => [\n \'placeholder\' => \'Ordre d\\\'importance\',\n \'min\' => \'1\',\n \'step\' => \'1\'\n ]\n ])\n ->add(\'modele\', TextType::class, [])\n\n ->add(\'codeReference\', TextType::class, [\n\n \'required\' => false,\n ])\n\n ->add(\'dateDeAjout\', DateType::class, [\n\n \'data\' => new \\DateTime(),\n ])\n\n ->add(\'cout\', NumberType::class, [\n\n \'attr\' => [\n \'placeholder\' => \'Co\xc3\xbbt\',\n \'min\' => \'0\',\n \'step\' => \'0.01\'\n ]\n ])\n\n ->add(\'dateDeAchat\', DateType::class, [\n\n \'data\' => new \\DateTime(),\n ])\n\n ->add(\'type\')\n\n ->add(\'locationDeAchat\');\n }\n\n public function configureOptions(OptionsResolver $resolver)\n {\n $resolver->setDefaults([\n \'data_class\' => Achats::class,\n ]);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n请,如果有人有建议,我将不胜感激。
\n年_月
是MySQL中的保留字,因此如果不进行特殊处理,不能用作列名
您需要在查询中将其括在反引号中,如下所示
`year_month`
Run Code Online (Sandbox Code Playgroud)
所以你的查询是
`year_month`
Run Code Online (Sandbox Code Playgroud)
当您使用 Symfony 时,我假设您也使用 Doctrine,在这种情况下,您需要告诉 ORM在其生成的查询中使用反引号