我有一个问题。我不知道它来自哪里。 我是usgin symfony 3.1,我使用Doctrine作为ORM。当我在bdd上请求我没有错误。但是当我想插入时我有这个错误
An exception occurred while executing 'INSERT INTO order (status, part_id, user_id) VALUES (?, ?, ?)' with params [1, "8", "1"]:
对我来说,学说有一个错误,但我没有找到它的起源.......
这是我的控制器动作
/**
* @Route("/order/new", name="new_order")
*/
public function newAction(Request $request)
{
if (false === $this->container->get('security.authorization_checker')->isGranted('ROLE_USER')){
throw new AccessDeniedException();
}
$order = new Order();
$form = $this->createForm(OrderType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$order->setUser($this->container->get('security.token_storage')->getToken()->getUser());
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
return $this->render('BEBundle:Order:new.html.twig', array(
'form' => $form->createView(),
));
}
这是我的实体
/**
* @ORM \ Table(name =“order”) * @ORM \ Entity(repositoryClass =“BEBundle \ Repository \ OrderRepository”) * / 班级订单 {
CONST ORDER_CREATED = 1;
CONST ORDER_TO_DELIVERY = 3;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="bigint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer $status
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @var Part $part
*
* @ORM\ManyToOne(targetEntity="Part", inversedBy="orders")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="part_id", referencedColumnName="id")
* })
*/
private $part;
/**
* @var User $user
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="orders")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
/ ...然后是getter和setter
部分订单
/**
* @var ArrayCollection $orders
*
* @ORM\OneToMany(targetEntity="Order", mappedBy="part")
*/
private $orders;
用户的订单
/**
* @var ArrayCollection $orders
*
* @ORM\OneToMany(targetEntity="Order", mappedBy="user")
*/
private $orders;
我不知道为什么会有这个错误。如果有人知道为什么会出现这个错误?
编辑表格类型
class OrderType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('part',EntityType::class , array(
'class' => Part::class,
'choice_label' => 'concat',
'label' => 'Part to order',
'error_bubbling' => true,
'required' => true,
)
)
;
}
/**
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BEBundle\Entity\Order',
));
}
/**
* @return string
*/
public function getName()
{
return 'BEBundle_Order';
}
}
答案 0 :(得分:2)
ORDER是mysql或doctrine的保留字。这就是为什么插入工作不起作用的原因。被迫用其他名称重命名我的班级订单。现在它正在工作!
答案 1 :(得分:0)
你可以在你的控制器中执行此操作以获得表单无效的原因吗?
if ($form->isSubmitted() && $form->isValid()) {
$order->setUser($this->container->get('security.token_storage')->getToken()->getUser());
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
else
{
$formErrors = $form->getErrors(true);
$errors = [];
foreach($formErrors as $key => $error)
{
$errors[$key] = $error->getMessageTemplate()."-";
}
$fichier = fopen("testFormErrors.txt", "w+");
fputs($fichier, print_r($errors, true));
fclose($fichier);
}
//Do what ever you what
所以基本上你应该在testFormErrors.txt文件中得到表单错误 如果它没有帮助,您是否也可以向我们提供您的OrderType类?