2014-01-14 1 views
3

symfony2에서 동일한 데이터베이스 테이블 이름으로 엔티티를 확장하는 데 문제가 있습니다. symfony에서 엔티티를 확장하려고하는데 기본 엔티티를 재사용 할 수 있어야하므로 항상 확장되지는 않습니다.Doctrine2 Symfony2 다른 번들이지만 동일한 데이터베이스 테이블 이름을 가진 엔티티 확장

이것은 현재 가지고있는 간단한 예입니다. 내 고객 엔티티 :

namespace Bundle\Entity\Customer; 

/** 
* @ORM\Table(name="customer") 
* @ORM\Entity() 
*/ 
class Customer implements CustomerInterface, UserInterface 
{ 
    //implementing variables and getters/setters 
} 

연재 엔티티 (다른 번들) :

namespace AnotherBundle\Entity\Customer; 

use Bundle\Entity\Customer\Customer as BaseCustomer; 

/** 
* @ORM\Entity() 
*/ 
class Customer extends BaseCustomer 
{ 
    //implementing variables and getters/setters 
} 

CustomerInterface : 내 config.yml에서

namespace Bundle\Model\Customer; 

interface CustomerInterface 
{ 
    // public methods of the first Customer class 
} 

나는 다음과 같은 규칙이 :

resolve_target_entities: 
     Bundle\Model\Customer\CustomerInterface: AnotherBundle\Entity\Customer\Customer 
나는 다음과 같은 오류 얻을 SQL 생성 할 때은 :

[Doctrine\DBAL\Schema\SchemaException] 
The table with name 'customer' already exists. 

을 나는 첫 번째 (기본) 개체를 확장하고 데이터베이스 테이블 이름을 유지하기 위해 두 번째 개체가 필요합니다. 하지만 내가 첫 번째 (기본) 엔티티를 확장하지 않으면이 엔티티가 여전히 자체에서 작동하기를 원합니다.

(엔티티가 실제로는 여전히 나에게 중복 된 테이블 이름 오류를 준 바로 하나에 매핑되었지만, 작동하지 않았다) Creating portable Bundles with extendable entities in Symfony2가 :

나는이 소스를 시도했지만 그들은 내 문제를 해결할 수

또한 doctrine의 상속 매핑이 도움이되지 않는 것 같습니다 (http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html)

오류를 이해하지만 두 엔터티 (서로 확장)가 동일한 데이터베이스 테이블에 쓸 수 없어야합니다

답변

1

상속은 y에서 구성되지 않습니다. 우리의 예. 당신은 당신이 원하는 것을 할 single table inheritance을 설정해야합니다 :

/** 
* @Entity(name="customer") 
* @InheritanceType("SINGLE_TABLE") 
* @DiscriminatorColumn(name="discr", type="string") 
* @DiscriminatorMap({"customer1" = "Namespace\To\First\Bundle\Customer", "customer2" = "Namespace\To\Another\Bundle\Customer"}) 
*/ 
class Customer implements CustomerInterface, UserInterface 
{ ... } 

그런 다음, 두 번째 고객 클래스 첫 번째 확장을 계속, 그 작동합니다.

+0

내 고객 테이블에 여분의 필드가 생겨 문제가 생길 수 있습니다. 난 여분의 discriminator 필드없이 단일 테이블을 만들 수있는 방법이 있었으면 좋겠지 만,이 방법으로 작동합니다. 감사! –

관련 문제