我该如何处理Symfony2 / Doctrine异常

时间:2014-07-29 03:14:45

标签: php symfony exception exception-handling doctrine-orm

也许这是一个热门话题,其他一些人也在讨论这个问题,但我还没有找到解决这个问题的好方法。以UNIQUE字段为例,请记住此错误。当我尝试将相同的值插入数据库时​​,我收到此错误:

  

SQLSTATE [23000]:完整性约束违规:1062重复条目   ' j1234567'对于关键' UNIQ_FC3A5A1592FC23A8'

当然这发生在app_dev.php (development)环境中,但我不知道如何处理这个问题,以便向用户显示错误页面而不是这个丑陋的错误。我在production测试了相同的代码,然后丑陋的错误消失了,但我得到了这个:

  

错误:内部服务器错误

路径,我,不止一个,例如,我可以在插入之前或通过AJAX发送请求之前检查记录的存在,但我想学习如何通过使用Symfony2和Doctrine2断言来实现这一点。我已将此代码添加到我的实体中:

<?php

....
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * SysPerfil
 *
 * @ORM\Entity
 * @ORM\Table(name="sys_perfil")
 * @UniqueEntity(fields={"rif"}, message="Este RIF ya existe en nuestra base de datos")
 * @UniqueEntity(fields={"ci"}, message="Este CI ya existe en nuestra base de datos")
 * @UniqueEntity(fields={"nombre"}, message="Este nombre ya existe en nuestra base de datos")
 */
class SysPerfil
{
    ....

但由于我收到上述错误,它无法正常工作,那么处理此错误的最佳方法是什么?有任何想法吗?建议?文档?

添加表单类型

是的,我通过表单类型发送数据,见下文:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);
    $builder
            ->add('email', 'email', array(
                'required' => true,
                'label' => 'Email',
                'trim' => true
            ))
            ->add('password', 'password', array(
                'required' => true,
                'label' => 'Contraseña',
                'always_empty' => true
            ))
            ->add('confirm', 'password', array(
                'required' => true,
                'mapped' => false,
                'label' => 'Verificar contraseña',
                'always_empty' => true
            ))
            ->add('enabled', 'checkbox', array(
                'required' => true,
                'label' => 'Activo?',
                'data' => true
            ))
            ->add('perfil', new AdminPerfilType());
}

AdminPerfilType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);
    $builder
            ->add('persJuridica', 'choice', array(
                'choices' => RifType::getChoices(),
                'required' => true,
                'label' => 'RIF',
                'trim' => true,
                'attr' => array(
                    'class' => 'persJuridica'
                )
            ))
            ->add('roleType', 'choice', array(
                'choices' => AdminRoleType::getChoices(),
                'required' => true,
                'label' => "Tipo de Usuario",
                'trim' => true
            ))
            ->add('rif', 'text', array(
                'required' => true,
                'label' => false,
                'trim' => true,
                'attr' => array(
                    'class' => "numeric",
                    'maxlength' => 15
                )
            ))
            ->add('ci', 'text', array(
                'label' => 'CI',
                'trim' => true,
                'attr' => array(
                    'class' => "numeric ci",
                    'disabled' => 'disabled'
                )
            ))
            ->add('nombre', 'text', array(
                'required' => true,
                'label' => 'Nombre',
                'trim' => true
            ))
            ->add('apellido', 'text', array(
                'required' => true,
                'label' => 'Apellidos',
                'trim' => true
            ));
}

如果您正在寻找表单中的验证规则,那么我还没有,因为我虽然Doctrine / Symfony2已经处理了那部分

1 个答案:

答案 0 :(得分:1)

我猜您的错误是因为您有父母 - &gt;具有一对一映射的子实体,您的表单验证是检查父实体验证规则而不检查子验证规则,因为您没有使用Assert\Valid

Symfony文档示例http://symfony.com/doc/current/reference/constraints/Valid.html

// src/Acme/HelloBundle/Entity/Address.php
namespace Acme\HelloBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Address
{
    /**
     * @Assert\NotBlank()
     */
    protected $street;

    /**
     * @Assert\NotBlank
     * @Assert\Length(max = "5")
     */
    protected $zipCode;
}

// src/Acme/HelloBundle/Entity/Author.php
namespace Acme\HelloBundle\Entity;

class Author
{
    /**
     * @Assert\NotBlank
     * @Assert\Length(min = "4")
     */
    protected $firstName;

    /**
     * @Assert\NotBlank
     */
    protected $lastName;

    //without this Symfony won't check if the inserted address is satisfying the validation rules or not 
    /**
     * @Assert\Valid
     */
    protected $address;
} 
相关问题