2014-02-14 2 views
0

OK, 이제 Doctrine2 ORM을 새로운 Zend2 프로젝트에서 사용하기로 결정 했으므로 모델 및 엔티티 클래스를 별도로 유지 관리하는 데 가장 좋은 설계 방법에 관한 질문이 있습니다. 나는 이 아니고, 엔티티와 모델의 차이점을 물어 보았습니다. 또한 Repository 클래스와 연결된 서비스 레이어를 정상적으로 작동하도록했습니다. 내가 묻는 것은, 비즈니스 클래스가있는 Model 클래스가 있고, Document 클래스를 말하고, 그런 다음 영속성을 나타내는 DocumentEntity를 분리 한 상태로 유지하려는 경우입니다. 예를 들어, 여기 엔 Entity가 있습니다.내 모델을 Doctrine2의 엔티티와 분리하는 것이 가장 좋은 방법은 무엇입니까?

class Document implements SomeImportantInterface, AnotherImportantInterface { 

    public function doSomeImportantStuff() { 
    } 

    public function doEvenMoreImportantStuff() { 
    } 
} 

그리고 마지막으로, 서비스 클래스 :

class DocumentService { 
    const DOCUMENT_ENTITY = 'DocumentEntity'; 

    /** 
    * @var EntityManager 
    */ 
    protected $em; 

    /** 
    * @param EntityManager $entityManager 
    */ 
    public function __construct(EntityManager $entityManager) { 
     $this->em = $entityManager; 
    } 


    public function getDocument($guid) { 
     $documentEntity = $this->em->getRepository(self::DOCUMENT_ENTITY)->findByGuid($guid); 

     return; //what? I want here Document to be returned, not the DocumentEntity.. 
    } 

    public function createDocument() { 
     return new Document(); 
    } 

    public function saveDocument(Document $document) { 
     // Document -> DocumentEntity 

     // $documentEntity = ...? 
     //$this->em->persist($entityDocument); 
     //$this->em->flush(); 
    } 
} 

S 자

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity(repositoryClass="DocumentRepo") 
* @ORM\Table(name="document") 
*/ 
class DocumentEntity { 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    * @ORM\Column(type="guid") 
    */ 
    protected $guid; 

    /** 
    * @ORM\Column(length=64) 
    */ 
    protected $name; 

    // more stuff below... 
} 

, 나는 별도의 유지하려는 비즈니스 로직의 많은 내 모델 클래스있다 o 볼 수 있듯이,이 계획은 DocumentEntities가 아닌 응용 프로그램에서만 사용할 수있는 Document 객체 (서비스를 통해 액세스 할 수 있음)를 갖는 것입니다. 내 마음에 온 두 가지 가능한 방법은 :
  • 은, 어쩌면 내가 여기에 뭔가를 누락, DocumentEntity

을 확장하는 문서

  • 메이크업 문서 안에 속성으로 DocumentEntity을 유지 또는 막대기의 잘못 끝내는거야? 앞으로 귀하의 의견을 듣고 싶습니다!

  • 답변

    0

    엔티티는 모델 클래스를 확장해야합니다. 애플리케이션은 저장소 인터페이스를 통해 모든 모델 상호 작용을 수행해야합니다.

    DomainModel 
        DocumentDomainModel 
        DocumentDomainModelInterface 
        DocumentDomainModelRepositoryInterface (get/create/save) 
    
    DoctrineEntity 
        DocumentDoctrineEntity extends DocumentDomainModel 
        DocumentDoctrineRepository implements DocumentDomainModelRepositoryInterface 
    

    이렇게하면 테스트 용 메모리 같은 다른 리포지토리를 유연하게 구현할 수 있습니다.

    관련 문제