让我有两个实体college
和department
,大学有许多部门(一个一对下)。我的验证。看起来像
AppCollegeBundleEntityCollege:
- departments:
- Valid: ~
AppCollegeBundleEntityDepartment:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity:
fields: [department_name, HOD ]
当我试图与N-N-NMBER部门Unique Entity
验证合作的大学实体持续下去时,稍后我试图将另一个新的department
实体添加到college
实体中,并坚持unique entity
验证失败,我遇到了unique key violation
,我犯了什么错误,我犯了什么错误?
我在CSV文件的每一行中使用csv import
功能,例如,我在控制器中进行了一个ajax调用
$department = new Department()
if( isset( $record["department_name"] ) && strlen( trim( $record["department_name"] ) ) > 0 )
{
$department->setDepartmentName($record["department_name"] );
}
if( isset( $record["hod"] ) && strlen( trim( $record["hod"] ) ) > 0 )
{
$department->setHod($record["hod"] );
}
//set college
$department->setCollege($college)
//adding departments
$college->addDepartment($department)
$validate = $this->get('validator')->validate($college);
if($validate->count())
{
return [ "status" => "error" ]
}
else
{
$em = $this->getDoctrine()->getManager();
$em->persist($college);
$em->flush();
return [ "status" => "success" ];
}
如果您不创建新的大学(因此您已经拥有大学实体),但只有一个新的部门尝试以下方法:
//set college
$department->setCollege($college)
$validate = $this->get('validator')->validate($department);
if($validate->count())
{
return [ "status" => "error" ]
}
else
{
$em = $this->getDoctrine()->getManager();
$em->persist($department);
$em->flush();
return [ "status" => "success" ];
}
相反,如果您正在创建一所新大学,我也建议您首先创建一所新大学,坚持下去并冲洗,拿起ID并创建新的部门