2017-02-20 36 views
2

我一個事實真在實體後,我其實架構更新--force幾個屬性和當我實際上添加數據庫中的數據存在,我覺得這是錯誤唯一真正的限制是不是行之有效

SQLSTATE [23000]:完整性約束違規:1062重複條目 '1' 關鍵 'UNIQ_101C7E5A6C6E55B5'

代碼實體:

<?php 

    namespace test\MedBundle\Entity; 

    use Doctrine\ORM\Mapping as ORM; 
    use Symfony\Component\Validator\Constraints as Assert; 
    use Symfony\Component\HttpFoundation\File\UploadedFile; 
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; 


    /** 
    * Apps 
    * 
    * @ORM\Table(name="apps",uniqueConstraints={@ORM\UniqueConstraint(columns={"nom", "prenom","age","class"})} 
    ) 
    * @ORM\Entity(repositoryClass="test\MedBundle\Repository\AppsRepository") 
    * @ORM\HasLifecycleCallbacks() 
    * @UniqueEntity(fields={"nom","prenom","class","age"}, message="Cette champ existe déja.") 

    */ 
    class Apps 
    { 
     /** 
     * @var int 
     * 
     * @ORM\Column(name="id", type="integer") 
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="AUTO") 
     */ 
     private $id; 

     /** 
     * @var string 
     * 
     * @ORM\Column(name="nom", type="string", length=255, unique=true) 
     */ 
     private $nom; 

     /** 
     * @var string 
     * 
     * @ORM\Column(name="class", type="string", length=255, unique=true) 
     */ 
     private $class; 

     /** 
     * @var string 
     * 
     * @ORM\Column(name="prenom", type="string", length=255, unique=true) 
     */ 
     private $prenom; 

     /** 
     * @var string 
     * 
     * @ORM\Column(name="age", type="string", length=255, unique=true) 
     */ 
     private $age; 


      /** 
     * @ORM\Column(type="string", length=255) 
     */ 
     public $path; 

     /** 
     * @Assert\File(maxSize="6000000") 
     */ 
     private $file; 


     /** 
     * @ORM\ManyToOne(targetEntity="Med",inversedBy="apps") 
     * @ORM\JoinColumn(name="id_med", referencedColumnName="id",nullable=true) 
     */ 
     private $med; 

     /** 
     * @ORM\OneToMany(targetEntity="test\MedBundle\Entity\Affichage",mappedBy="apps") 
     */ 
     private $comment; 

     /** 
     * @ORM\Column(name="updated_at", type="datetime", nullable=true) 
     */ 

     private $updatedAt; 

     /** 
     * @ORM\Column(name="nbr_modif", type="string", nullable=true) 
     */ 

     private $unbr_modif; 


     /** 
     * @ORM\PreUpdate 
     */ 
     public function nbremodif() { 
      $this->setUnbrModif($this->unbr_modif+1); 
     } 

    } 

我控制器

public function addAction() { 
     $em = $this->getDoctrine()->getManager(); 
     $request = $this->getRequest(); 

     if ($request->getMethod() == 'POST') { 
     $x= $request->request->get('k'); 

      for($i=1;$i<=$x;$i++){ 

     $nom = $request->request->get('nom'.$i); 
     $prenom = $request->request->get('prenom'.$i); 
     $age = $request->request->get('age'.$i); 
     $class = $request->request->get('sexe'.$i); 

      $cop = new Apps(); 
      $cop->setAge($age); 
      $cop->setNom($nom); 
      $cop->setPrenom($prenom); 
      $cop->setClass($class); 
      $cop->setPath("path test"); 
      $em->persist($cop); 
      $em->flush(); 
      } 
      } //end for 

     return $this->render('MedBundle:Apps:form.html.twig'); 


     } 

是什麼時,他說,我們看到警報的消息告訴他,這個數據已經存在於數據庫中的數據

回答

1

如果你想在多個領域的唯一約束你使用UniqueEntity爲您提供解決方案應:

@UniqueEntity(fields={"nom","prenom","class","age"}, message="...") 

您應該刪除unique=true因爲這種約束適用於一個獨特的領域(而不是多重約束)

可以找到一個例子in the doc

/** 
* @ORM\Entity 
* @UniqueEntity(
*  fields={"host", "port"}, 
*  errorPath="port", 
*  message="This port is already in use on that host." 
*) 
*/ 
class Service 
{ 
    /** 
    * @ORM\ManyToOne(targetEntity="Host") 
    */ 
    public $host; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    public $port; 
} 

然後,觸發使用Assert(如UniqueEntity),您可以使用validator service[doc]驗證。

如果你做一個形式,該服務將自動調用

/*... $cop->setPath("path test"); ...*/ 
$validator = $this->get('validator'); 
$errors = $validator->validate($cop); 

if (count($errors) > 0) { 
    /* 
    * Uses a __toString method on the $errors variable which is a 
    * ConstraintViolationList object. This gives us a nice string 
    * for debugging. 
    */ 
    $errorsString = (string) $errors; 

    return new Response($errorsString); 
} else { 
    $em->persist($cop); 
    $em->flush(); 
} 
+0

同樣的問題我的朋友 這就是我現在發現 SQLSTATE [23000]:完整性約束違規:1062 Duplicata杜冠軍「1-1 -1-1'pour la clef'UNIQ_101C7E5A6C6E55B5A625945BA13010B2ED4B199F' –

+0

這不完全是同一個問題;)你如何在控制器中綁定和驗證你的表單? – goto

+0

代碼我的控制器: http://pastebin.com/LgDCRatq –

相關問題