2016-03-19 148 views
0

我意識到我的錯誤,但我不知道如何解決它。 我有兩個表,汽車和廣告,我希望執行一個嵌套的表格來添加數據到表中。但是我有一個錯誤信息,說:執行Symfony完整性約束違規:1062重複條目'1'的鍵

INSERT INTO car (year, model, engine_size, colour, mileage, advertisement_id, fuel_id, transmission_id, body_id, make_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 

使用參數["2012", "a3", "v8", "45", 4, 26, 1, 1, 1, 1]發生

例外。

例外:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_773DE69D78D28519' 

我的問題是:爲什麼我的方法不找回我的實體的id把它在實體廣告?

關係

/** 
* 
* @ORM\OneToOne(targetEntity="Home\HomeBundle\Entity\Advertisement", cascade={"persist","remove"}) 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $Advertisement; 

public function setAdvertisement(\Home\HomeBundle\Entity\Advertisement $advertisement) 
{ 
    $this->Advertisement = $advertisement; 

    return $this; 
} 

class CarType extends AbstractType 
{ 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('year') 
     ->add('model') 
     ->add('engineSize') 
     ->add('colour') 
     ->add('mileage') 
     ->add('Advertisement', new AdvertisementType()) 
     ->add('Fuel') 
     ->add('Transmission') 
     ->add('Body') 
     ->add('Make') 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Home\HomeBundle\Entity\Car' 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'home_homebundle_car'; 
} 
} 

控制器

public function createAction(Request $request) 
{ 
    $entity = new Car(); 
    $form = $this->createCreateForm($entity); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('car_show', array('id' => $entity->getId()))); 
    } 

    return $this->render('HomeBundle:Car:new.html.twig', array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    )); 
} 

/** 
* Creates a form to create a Car entity. 
* 
* @param Car $entity The entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createCreateForm(Car $entity) 
{ 
    $form = $this->createForm(new CarType(), $entity, array(
     'action' => $this->generateUrl('car_create'), 
     'method' => 'POST', 
    )); 

    $form->add('submit', 'submit', array('label' => 'Create')); 

    return $form; 
} 

回答

0

一對一關係,並且您的數據庫中具有相同ID的實體。

相關問題