我是Symfony的初學者。我的問題是,我試圖給我的基礎MySQL添加值,問題是我有相同的ID,但我驗證了我的實體,我生成值@ORM/GeneratedValues
,我不明白我的錯在哪裏。主義堅持:
路由:
esprit_parc_AjoutVoiture:
path: /Ajout_voiture/
defaults: { _controller: ParcBundle:Voiture:add }
我的控制器:
public function addAction (Request $Request)
{
$Voiture = new Voiture();
$form = $this->createForm(VoitureType::class,$Voiture);
$form->handleRequest($Request);
if ($form->isValid())
{
$em=$this->getDoctrine()->getManager();
$em->persist($Voiture);
$em->flush();
return $this->redirect($this->generateUrl(
'esprit_parc_Affichage'
));
}
return $this->render(
'ParcBundle:Voiture:ajout.html.twig',
array('form'=>$form->createView()
));
}
}
實體VOITURE:
<?php
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class voiture
* @package ParcBundle\Entity
* @ORM\Entity
* @ORM\Table(name="Voiture")
*/
class Voiture
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="string",length=255)
*
*/
private $Serie;
/**
* @ORM\Column(type="datetime",length=255)
*/
private $DateMiseCirculation;
/**
* @ORM\Column(type="string",length=255)
*/
private $Marque;
/**
* @ORM\ManyToOne(targetEntity="ParcBundle\Entity\Modele")
* @ORM\JoinColumn(name="id", referencedColumnName="id");
*/
private $modele;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getModele()
{
return $this->modele;
}
public function setModele($modele)
{
$this->modele = $modele;
}
public function getSerie()
{
return $this->Serie;
}
public function setSerie($Serie)
{
$this->Serie = $Serie;
}
public function getDateMiseCirculation()
{
return $this->DateMiseCirculation;
}
public function setDateMiseCirculation($DateMiseCirculation)
{
$this->DateMiseCirculation = $DateMiseCirculation;
}
public function getMarque()
{
return $this->Marque;
}
public function setMarque($Marque)
{
$this->Marque = $Marque;
}
}
錯誤:
An exception occurred while executing 'INSERT INTO Voiture (serie, date_mise_circulation, marque, id) VALUES (?, ?, ?, ?)' with params ["2313", "2012-12-03 02:02:00", "sd", 1]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'
PS:我的值爲id = "1"
,它應該自動增加id。
[編輯]:VoitureType
類:
<?php
namespace ParcBundle\Form;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class VoitureType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('Serie')
->add('DateMiseCirculation')
->add('Marque')
->add('modele', EntityType::class, array(
"class" => "ParcBundle:Modele",
"choice_label"=> "libelle"
))
->add("Ajouter",SubmitType::class);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ParcBundle\Entity\Voiture'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'parcbundle_voiture';
}
}
在你的'VoitureType'中,你有字段'id'嗎? –
我用命令生成的,我會在這裏粘貼1分鐘 –
它假設一個自動生成,我想我不需要把它放在voitureType –