Mysql Uniquentity验证失败-Symfony?
比如说,我有两个实体Mysql Uniquentity验证失败-Symfony?,mysql,symfony,doctrine-orm,Mysql,Symfony,Doctrine Orm,比如说,我有两个实体学院和系,其中学院有多个系(一对多)。我的validation.yml看起来像 App\CollegeBundle\Entity\College: - departments: - Valid: ~ App\CollegeBundle\Entity\Department: constraints: - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEnti
学院
和系
,其中学院有多个系(一对多)。我的validation.yml看起来像
App\CollegeBundle\Entity\College:
- departments:
- Valid: ~
App\CollegeBundle\Entity\Department:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [department_name, HOD ]
当我尝试使用n个部门数来持久化学院实体时,唯一实体
验证有效,之后当我尝试将另一个新的部门
实体添加到学院
实体时,持久化唯一实体
验证失败,我得到了唯一密钥冲突
,我犯了什么错误
我在csv文件的每一行上使用csv导入
功能,例如在我的控制器中调用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并创建一个新系如果您没有创建一所新学院(因此您已经有一个学院实体),但只有一个新系尝试以下方法:
//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并创建新系请在保存时向我们显示代码please@AlessandroMinoccheri我添加了代码请在保存时显示代码please@AlessandroMinoccheri我添加了代码