当我打算保存数据库允许的数据@uniqueConstrainst时,我找不到处理数据库错误的方法。 问题是我的@UniqueEntity在显示数据库错误之前没有处理错误。
这是我的代码
* @ORM\Table(name="persona_idioma", uniqueConstraints={@UniqueConstraint(name="persona_idioma_unique",columns={"id_persona","id_idioma"})},indexes={@ORM\Index(name="IX_Relationship11", columns={"id_idioma"}), @ORM\Index(name="IX_Relationship12", columns={"id_persona"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\PersonaIdiomaRepository")
* @UniqueEntity(
* fields={"idPersona", "idIdioma"},
* errorPath = "idPersona",
* message="mensaje"
*
* )
更新:我刚刚添加了实体的列
/**
* @var \AppBundle\Entity\Persona
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Persona",inversedBy="idiomas")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_persona", referencedColumnName="id_persona")
* })
*/
private $idPersona;
/**
* @var \AppBundle\Entity\Idioma
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Idioma")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_idioma", referencedColumnName="id_idioma")
* })
*/
private $idIdioma;
实体持久化的控制器
$formulario->handleRequest($request);
if ($formulario->isSubmitted() && $formulario->isValid()) {
$em->persist($persona);
$em->flush();
return $this->redirectToRoute('persona_edit', array('id_persona' => $persona->getIdPersona()));
}
return $this->render('persona/new_edit.html.twig', [
'formulario' => $formulario->createView(),
]);
答案 0 :(得分:-1)
我和你有同样的问题,我改变了我的代码:
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity(fields={"username", "email"}, message="This value is already taken")
*/
为:
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity(fields={"username"}, message="This username is already taken")
* @UniqueEntity(fields={"email"}, message="This email is already taken")
*/
这很奇怪,但它对我有用。如果我帮助你,请给我签名。