2012-04-18 4 views
0

많은 변수가있는 클래스가 있습니다. 그들 중 몇몇은 파일 형식이다symfony2 필드의 값이 같지 않음

class Proposal { 
/** 
* @ORM\ManyToOne(targetEntity="File") 
* @ORM\JoinColumn(name="clientCommFile_id", referencedColumnName="id") */ 
private $clientCommFile; 

/** 
* @ORM\ManyToOne(targetEntity="File") 
* @ORM\JoinColumn(name="contractFile_id", referencedColumnName="id") 
*/ 
private $contractFile; 

/** 
* @ORM\ManyToOne(targetEntity="File") 
* @ORM\JoinColumn(name="proposalFile_id", referencedColumnName="id") 
*/ 
private $proposalFile; 

내가 필요로하는 파일의 ID를 검사 할 유효성을 검사 할 몇 가지 유효성 검사 규칙을 만들 않습니다. ID는 데이터베이스의 파일을 참조하기 때문에 달라야합니다. 어떨까요, 어떻게해야합니까?

답변

1

그래서 clientCommFile, contractFile 및 proposalFile을 다르게 지정 하시겠습니까? 객체를 비교할 수 있다고 가정하면 다음과 같습니다.

use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\Validator\ExecutionContext; 

/** 
* @Assert\Callback(methods={"areFilesValid"}) 
*/ 
class Proposal { 

    // .. 

    public function areFilesValid(ExecutionContext $context) { 
     if($this->clientCommFile != null && $this->clientCommFile->equals($this->contractFile)){ 
      $propertyPath = $context->getPropertyPath() . '.options'; 
      $context->setPropertyPath($propertyPath); 
      $context->addViolation('ClientCommFile and ContractFile are equal', array(), null); 
     } 
    } 
} 

물론 equals 메소드를 객체에 구현해야합니다.

더 복잡해진다 또는 데이터베이스 액세스가 필요한 경우는, custom validators

+0

데이터베이스 액세스에 대해 살펴 여기에 필요하지 않습니다 있습니다. 나는 형식에서 얻은 파일 ID를 어떻게 든 공존시킬 수 있습니까? 제 의도는 배열에 ID를 붙이고 오름차순으로 정렬하고 i-1 색인이 i 색인과 일치하도록합니다. 콜백 메서드 대신 getter 메서드를 사용했지만 작동하지 않습니다. –

관련 문제