0
votes

I have an issue. and I don't know from where it comes. I'm usgin symfony 3.1, and i use Doctrine as ORM. When i request on the bdd i have no error. But when i want to insert I have this error

An exception occurred while executing 'INSERT INTO order (status, part_id, user_id) VALUES (?, ?, ?)' with params [1, "8", "1"]:

For me there is an error with doctrine but i don't succeed to locate the origin of it ....

Here is my controller action

    /**
 * @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(),
    ));

}

Here is my entity

/**

* @ORM\Table(name="order") * @ORM\Entity(repositoryClass="BEBundle\Repository\OrderRepository") */ class Order {

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;

/... Then getter and setter

the orders in part

 /**
 * @var ArrayCollection $orders
 *
 * @ORM\OneToMany(targetEntity="Order", mappedBy="part")
 */
private $orders;

And the orders in user

    /**
 * @var ArrayCollection $orders
 *
 * @ORM\OneToMany(targetEntity="Order", mappedBy="user")
 */
private $orders;

I don't know why there is this mistake. if anyone has the idea of why this error ?

EDIT FORM TYPE

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';
}

}

2
Your part_id and user_id are double quoted. Shouldn't these be integers? - Chris
they are integer, it's the id of the corresponding object in the bdd. Should i put only simple quotes ? - T3ddy
I started to change few things, now my object id's are integer - T3ddy

2 Answers

2
votes

ORDER is a reserved word for mysql or for doctrine. That's why the insert wasn't working. Was forced to rename my class order by an other name. And now it's working !

0
votes

can you do this in your controller in other to get the reason why your form is not valid?

 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

So basically you should get the form error in testFormErrors.txt file If it does not help, can you also provide us your OrderType class?