我在Symfony 2.8.8项目中的UniqueEntity验证存在问题,该问题未被触发导致数据库异常: SQLSTATE [23000]:完整性约束违规:1062重复条目' 7Nrl / JfB8E47D1ZtoryFsQ =='对于关键' UNIQ_3BF9FEA896901F54'
其他验证规则有效。
这里是validation.yml。未触发约束的字段是" number"。
AppBundle\Entity\UrinaryCytologicalExam:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [number]
message: Il numero di referto è già presente nel db
properties:
age:
- Type:
type: integer
acceptanceDate:
- NotBlank:
message: Campo obbligatorio
- Date:
message: La data inserita non è valida
adequacySample:
- NotBlank:
message: Campo obbligatorio
- Type:
type: string
- Length:
min: 3
max: 255
minMessage: "Il campo deve contenere almeno {{ limit }} caratteri."
maxMessage: "Il campo non può superare {{ limit }} caratteri."
anatomicalSamplingSite:
- Type:
type: string
- Length:
min: 3
max: 255
minMessage: "Il campo deve contenere almeno {{ limit }} caratteri."
maxMessage: "Il campo non può superare {{ limit }} caratteri."
category:
- NotBlank:
message: Campo obbligatorio
- Valid: ~
clinicalInformation:
- NotBlank:
message: Campo obbligatorio
- Type:
type: string
description:
- NotBlank:
message: Campo obbligatorio
- Type:
type: string
number:
- NotBlank:
message: Campo obbligatorio
- Regex:
pattern: "/^(C|c)\s[0-9]{4}\s[0-9]{4}$/"
message: "Il numero di referto è composto dalla lettera C, uno spazio, 4 numeri, uno spazio, 4 numeri (es. C 2016 0008)"
reportDate:
- Date:
message: La data inserita non è valida
sampleDate:
- Date:
message: La data inserita non è valida
submittedMaterial:
- NotBlank:
message: Campo obbligatorio
- Type:
type: string
gynecologist:
- Valid: ~
以及其他必要文件......
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* AppBundle\Entity\UrinaryCytologicalExam
*
* @ORM\Table(name="urinary_cytological_exams")
* @ORM\Entity(repositoryClass="UrinaryCytologicalExamRepository")
* @ORM\HasLifecycleCallbacks
*/
class UrinaryCytologicalExam
{
CONST EXAM_NAME = 'Esame citologico urinario';
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int $age
*
* @ORM\Column(type="integer", nullable=true)
*/
private $age;
/**
* @var datetime $acceptanceDate
*
* @ORM\Column(name="acceptance_date", type="date", nullable = true)
*/
private $acceptanceDate;
/**
* @var string $adequacySample
*
* @ORM\Column(type="encrypted_string", length=255)
*/
private $adequacySample;
/**
* @var string $anatomicalSamplingSite
*
* @ORM\Column(type="encrypted_string", length=255)
*/
private $anatomicalSamplingSite;
/**
* @var string $clinicalInformation
*
* @ORM\Column(name="clinical_information", type="encrypted_text")
*/
private $clinicalInformation;
/**
* @var string $description
*
* @ORM\Column(type="encrypted_text")
*/
private $description;
/**
* @var boolean $isActive
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $isActive = false;
/**
* @var boolean $isSampleDateNotAvailable
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $isSampleDateNotAvailable;
/**
* @var string $number
*
* @ORM\Column(type="encrypted_string", length=255, unique = true)
*/
private $number;
/**
* @var datetime $sampleDate
*
* @ORM\Column(name="sample_date", type="date", nullable = true)
*/
private $sampleDate;
/**
* @var datetime $reportDate
*
* @ORM\Column(name="report_date", type="date", nullable = true)
*
*/
private $reportDate;
/**
* @var string $submittedMaterial
*
* @ORM\Column(name="submitted_material", type="encrypted_text")
*/
private $submittedMaterial;
/**
* @var datetime $createdAt
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
* @Gedmo\Timestampable(on="create")
*
*/
private $createdAt;
/**
* @var datetime $updatedAt
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @var Category $category
*
* @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Core\UrinaryCytologicalExamParisSystem")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* @var Gynecologist $gynecologist
*
* @ORM\ManyToOne(targetEntity="Gynecologist")
* @ORM\JoinColumn(name="gynecologist_id", referencedColumnName="id")
*/
private $gynecologist;
/**
* @var PersonDetail $personDetail
*
* @ORM\ManyToOne(targetEntity="\AppBundle\Entity\PersonDetail", inversedBy="HPVExams")
* @ORM\JoinColumn(name="person_detail_id", referencedColumnName="id")
*/
private $personDetail;
/**
* @var User $user
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $user;
//...
}
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\Form\FormInterface,
Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\UrinaryCytologicalExam;
use AppBundle\Utility\Uppercaser;
class UrinaryCytologicalExamFormHandler {
private $entityManager;
private $isClickedSubmitAndAddButton;
private $session;
private $uppercaser;
public function __construct(
EntityManager $entityManager,
Session $session,
Uppercaser $uppercaser
)
{
$this->entityManager = $entityManager;
$this->session = $session;
$this->uppercaser = $uppercaser;
}
public function handle(FormInterface $form, Request $request, $message)
{
if(!$request->isMethod('POST')) {
return false;
}
$form->bind($request);
$this->isClickedSubmitAndAddButton = ($form->get('submitAndAdd')->isClicked()) ? true : false;
if(!$form->isValid()) {
return false;
}
$exam = $form->getData();
$this->persist($exam, $message);
return true;
}
public function getIsClickedSubmitAndAddButton() {
return $this->isClickedSubmitAndAddButton;
}
public function persist(UrinaryCytologicalExam $exam, $message)
{
$this->entityManager->persist($this->uppercaser->uppercase($exam));
$this->entityManager->flush();
$this->session->getFlashBag()->add('success', $message);
}
}
相同的代码正在与其他实体合作......