我对单个实体中的一些属性是真实的,并且事实上架构更新后--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');
}
什么是解决方案,当他添加数据时,我们看到Alert的消息告诉他这个数据已经存在于数据库中
答案 0 :(得分: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();
}