2012-09-07 6 views
2

에서이 코멘트에 대해 약간의 설명을 드리겠습니다. Doctrine 2 자료로 작업 중이며 ObjectExists.phpNoObjectExists.php과 같은 유효성 확인을 위해 Doctrine 모듈에 착수했습니다.Doctrine 2 모듈 'ObjectExists.php'

내 질문은 here 찾을 수있는 원래 코드에서입니다.

나는 "$options 필요한 키를 Doctrine\Common\Persistence\ObjectRepository의 인스턴스 여야합니다 object_repository을이다"언급 여기에 사실을 얻을 수

Doctrine\Common\Persistence\ObjectRepository 이후

는 인터페이스가

/** 
    * Constructor 
    * 
    * @param array $options required keys are `object_repository`, which must be an instance of 
    * Doctrine\Common\Persistence\ObjectRepository, and `fields`, with either 
    * a string or an array of strings representing the fields to be matched by the validator. 
    * @throws \Zend\Validator\Exception\InvalidArgumentException 
    */ 
    public function __construct(array $options) 
    { 
     if (!isset($options['object_repository']) || !$options['object_repository'] instanceof ObjectRepository) { 
      if (!array_key_exists('object_repository', $options)) { 
       $provided = 'nothing'; 
      } else { 
       if (is_object($options['object_repository'])) { 
        $provided = get_class($options['object_repository']); 
       } else { 
        $provided = getType($options['object_repository']); 
       } 
      } 

      throw new Exception\InvalidArgumentException(sprintf(
       'Option "object_repository" is required and must be an instance of' 
        . ' Doctrine\Common\Persistence\ObjectRepository, %s given', 
       $provided 
      )); 
     } 

     $this->objectRepository = $options['object_repository']; 

     if (!isset($options['fields'])) { 
      throw new Exception\InvalidArgumentException(
       'Key `fields` must be provided and be a field or a list of fields to be used when searching for' 
        . ' existing instances' 
      ); 
     } 

     $this->fields = $options['fields']; 
     $this->validateFields(); 

     parent::__construct($options); 
    } 
, 어떻게 그 문을 디코딩한다 ?

또는 Doctrine\Common\Persistence\ObjectRepository의 인스턴스 여야합니다 내가 ObjectsExists 클래스의 생성자를 호출하고 object_repository 통과 할 수있는 방법 즉,

?

누군가가 이것에 대해 약간의 질문을 던질 수 있습니까? 저는이 문제에 익숙해 져 있습니다. 제 질문에 너무 가혹하지 마십시오.

감사 Doctrine\Common\Persistence\ObjectRepository 이후

답변

1

당신이 그것을 인스턴스화 할 수 있지만 당신이 그것을 구현할 수있는 인터페이스입니다.

class MyObjectRepository implements Doctrine\Common\Persistence\ObjectRepository 
{ 
    // ... 
} 

지금 MyObjectRepository의 모든 인스턴스는 ObjectsExistsobject_repository 요구 사항을 충족합니다.
new ObjectsExists(array(
    'object_repository' => new MyObjectRepository(), 
    // ... 
)); 

instanceof 소개 :

instanceof은 PHP 변수가 특정 클래스의 객체 인스턴스인지 결정하는데 사용된다.

instanceof 또한 변수가 상위 클래스에서 상속 한 클래스의 인스턴스화 된 개체인지 확인하는 데 사용할 수 있습니다.

instanceof 또한 변수가 인터페이스를 구현하는 클래스의 인스턴스화 된 개체인지 여부를 확인하는 데 사용할 수 있습니다.

0

Doctrine \ Common \ Persistence \ ObjectRepository를 구현하는 가장 일반적인 클래스는 Doctrine \ ORM \ EntityRepository입니다. 교리 ORM을 사용하는 경우

따라서는 object_repository 옵션 (명시 적으로 정의, 아니면 그냥 일반 사람은 당신이 $em->getRepository('User')에서 돌아 오기)

명시 적으로 정의되지 않은 것 이유는 당신의 UserRepository과 같이해야한다 [No] ObjectExists 유효성 검사기가 DoctrineORMModule에없고 DoctrineModule에 있기 때문입니다. 따라서 이 아닌 경우 ORM을 사용하는 경우 ObjectRepository를 구현하는 고유 한 UserRepository 클래스를 만들어야합니다.