2015-05-01 100 views
-1

我已经有一个图类,其中有ClassA(superClass)和两个类ClassBClassC,它们从ClassA继承。 如何使用Doctrine在Symfony2中对它们进行编码?Symfony2中的继承

注意:当我使用经典方法(我的意思是doctrine:generate:entity)生成实体时,我在我的数据库中有三个表。

回答

0

如果你想使用单台3类,你应该使用DiscriminatorMap

例子:我们有一类产品,以及两个子类优惠券和CustomProduct从产品

/** 
* MyApp\ProductBundle\Entity\Product 
* 
* @ORM\Table(name="product") 
* @ORM\Entity(repositoryClass="MyApp\ProductBundle\Repository\ProductRepository") 
* @ORM\InheritanceType("SINGLE_TABLE") 
* @ORM\DiscriminatorColumn(name="productType", type="string") 
* @ORM\DiscriminatorMap({ 
* "Product" = "Product", 
* "Coupon" = "MyApp\CouponBundle\Entity\Coupon", 
* "CustomProduct" = "MyApp\CustomProductBundle\Entity\CustomProduct", 
* }) 
*/ 
class Product implements PriceInterface 
{ 
... 
} 

MyApp的继承\ CouponBundle \实体\券

/** 
* @ORM\Entity 
*/ 
class Coupon extends Product implements ProductInterface 
{ 
} 

MyApp的\ CusomProductBundle \实体\ CustomProduct

/** 
* @ORM\Entity 
*/ 
class CustomProduct extends Product implements ProductInterface 
{ 
} 

它将允许对所有3个类使用公共表“product”,字段“productType”将存储记录的类名。更多信息http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

更新您的问题大约2个表:

可以指定为继承类独立的表:

MyApp的\ CouponBundle \实体\券

/** 
* @ORM\Table(name="coupon") 
* @ORM\Entity 
*/ 
class Coupon extends Product implements ProductInterface 
{ 
} 

MyApp的\ CusomProductBundle \ Entity \ CustomProduct

/** 
* @ORM\Table(name="custom_product") 
* @ORM\Entity 
*/ 
class CustomProduct extends Product implements ProductInterface 
{ 
} 
+0

如果我以这种方式编码我的实体。我会在数据库2或3中获得多少表格? –

+0

你将有1桌“产品” –

+0

是否有另一种方式,让我得到两个'表'优惠券和CustomProduct?的确,我想要在子类中找到超类属性,当我生成我的表格时 –