Doctrine2 Symfony2扩展不同捆绑包中但具有相同数据库表名的实体
在symfony2中扩展具有相同数据库表名的实体时遇到问题。 我试图在symfony中扩展一个实体,但是基本实体需要可重用,所以它不会总是被扩展 这是我目前拥有的一个简化示例。 我的客户实体:Doctrine2 Symfony2扩展不同捆绑包中但具有相同数据库表名的实体,symfony,doctrine-orm,Symfony,Doctrine Orm,在symfony2中扩展具有相同数据库表名的实体时遇到问题。 我试图在symfony中扩展一个实体,但是基本实体需要可重用,所以它不会总是被扩展 这是我目前拥有的一个简化示例。 我的客户实体: namespace Bundle\Entity\Customer; /** * @ORM\Table(name="customer") * @ORM\Entity() */ class Customer implements CustomerInterface, UserInterface {
namespace Bundle\Entity\Customer;
/**
* @ORM\Table(name="customer")
* @ORM\Entity()
*/
class Customer implements CustomerInterface, UserInterface
{
//implementing variables and getters/setters
}
扩展实体(在另一个捆绑包中):
CustomerInterface:
namespace Bundle\Model\Customer;
interface CustomerInterface
{
// public methods of the first Customer class
}
在my config.yml中,我有以下规则:
resolve_target_entities:
Bundle\Model\Customer\CustomerInterface: AnotherBundle\Entity\Customer\Customer
生成SQL时,出现以下错误:
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'customer' already exists.
我需要第二个实体来扩展第一个(基本)实体并维护数据库表名。但是当我不扩展第一个(基本)实体时,我仍然希望这个实体能够自己工作
我尝试了这个来源,但他们无法解决我的问题:
(不起作用,尽管实体确实映射到了正确的实体,但它仍然给了我重复的表名错误)
此外,条令的固有映射似乎没有帮助()
我理解这个错误,但是是否可以让两个实体(彼此扩展)写入同一个数据库表呢。您必须设置为执行您想要的操作:
/**
* @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
{ ... }
然后,让您的第二个客户类继续扩展第一个客户类,这应该会起作用。对我来说,最好的方法是忽略dbal级别的第二个实体。表将被创建, 并且不会出现错误。
另外,感谢Marco Pivetta、Alexandru Trandafir Catalin。问题在于它在我的客户表中创建了一个额外的字段,这在我的情况下是无用的。我希望有一种方法可以使它成为没有额外的鉴别器字段的单表,但它确实是这样工作的。谢谢
/**
* @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
{ ... }