我有问题。而且我不知道它的到来。我是USGIN SYMFONY 3.1,我将教义用作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(repositorityClass =" bebundle repository orderRepository")*/课堂顺序{
CONST ORDER_CREATED = 1;
CONST ORDER_TO_DELIVERY = 3;
/**
* @var integer $id
*
* @ORMColumn(name="id", type="bigint")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer $status
*
* @ORMColumn(name="status", type="integer")
*/
private $status;
/**
* @var Part $part
*
* @ORMManyToOne(targetEntity="Part", inversedBy="orders")
* @ORMJoinColumns({
* @ORMJoinColumn(name="part_id", referencedColumnName="id")
* })
*/
private $part;
/**
* @var User $user
*
* @ORMManyToOne(targetEntity="User", inversedBy="orders")
* @ORMJoinColumns({
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
/...然后getter and setter
部分中的订单
/**
* @var ArrayCollection $orders
*
* @ORMOneToMany(targetEntity="Order", mappedBy="part")
*/
private $orders;
和用户中的订单
/**
* @var ArrayCollection $orders
*
* @ORMOneToMany(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' => 'BEBundleEntityOrder',
));
}
/**
* @return string
*/
public function getName()
{
return 'BEBundle_Order';
}
}
订单是MySQL或学说的保留单词。这就是为什么插入不起作用的原因。被迫用其他名称重命名我的班级订单。现在它正在工作!
您可以在其他控制器中以其他方式执行此操作,以获取您的表格无效的原因?
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类吗?