2014-07-14 3 views
1

엔티티 자체 아래에 uniqueConstraints에 메시지를 첨부하려면 어떻게합니까? 아래의 두 번째 쿼리는 An exception occurred while executing....SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry...을 생성합니다. 이 비우호적 인 메시지 대신 "나는 무엇이든 무엇이든 요청하십시오 ..."를 어떻게 인쇄 할 수 있습니까?Symfony2의 UniqueConstraint 오류 메시지 처리

SQL 2INSERT INTO cars (model, brands_id) VALUES ('bmw', '5')

주 1INSERT INTO cars (model, brands_id) VALUES ('bmw', '5')

SQL : 나는 일반적으로 심포니 문서가 좋지 발견하고 대부분의 지름길이 될 것으로 보인다. 당신은 해결책을 찾고있는 누군가가 그 답을 알고 있다면 당신이있어하지 않을 경우 당신은 ....

ENTITY

/** 
* @ORM\Entity 
* @ORM\Table(name="cars", uniqueConstraints={@ORM\UniqueConstraint(columns={"model", "brands_id"})}) 
*/ 
class Cars 
{ 

CONTROLLER 행운을 경우

try 
{ 
    $submission = $form->getData(); 
    $em = $this->getDoctrine()->getManager(); 

    $cars = new Cars(); 
    $cars->setModel($submission->getModel()); 
    $cars->setBrands($submission->getBrands()); 

    $em->persist($cars); 
    $em->flush(); 

    ....... 
} 
catch (Exception $e) 
{ 
    ...... 
} 

형식 유형

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->setAction($options['action']) 
     ->setMethod('POST') 

     ->add('brands', 'entity', 
      array(
       'class' => 'CarBrandBundle:Brands', 
       'property' => 'name', 
       'multiple' => false, 
       'expanded' => false, 
       'empty_value' => '', 
       'query_builder' => function (EntityRepository $repo) 
            { 
             return $repo->createQueryBuilder('b')->orderBy('b.name', 'ASC'); 
            } 
      )) 

     ->add('model', 'text', array('label' => 'Model')) 
     ->add('button', 'submit', array('label' => 'Submit')) 
    ; 
} 
+0

양식 제출 후이 INSERT를 보냅니 까? – DonCallisto

+0

예 양식 유형에서 왔습니다. – BentCoder

+1

위에 참조로 양식 유형을 추가했습니다. – BentCoder

답변

5

validation.yml 파일을 src/Car/BrandBundle/Resources/config 폴더에 저장하십시오.

내용이 유사해야합니다 : 당신은 또한 검증 제약 조건을 사용하여 주석을 사용할 수

CAR\BrandBundle\Entity\Cars: 
    constraints: 
     - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: 
      fields: [model, brands_id] 
      message: "your_message_here" 

.

자세한 내용은 the documentation을 참조하십시오.

+0

젠장, 너 맞아 :) – DonCallisto

+0

두 번째 옵션에 대한 예로서 이것을 기억할 것이다. 그렇게 고맙다. 위의 솔루션을 주석에 추가했습니다. +1 그리고 나는 당신의 해결책을 받아들이고 있습니다. – BentCoder