Symfony2の継承

Youssef CHARIF

ClassA(superClass)と2つのクラスがClassBあり、ClassCから継承するダイアグラムクラスがすでにありClassAます。Doctrineを使用してSymfony2でそれらをコーディングするにはどうすればよいですか?

注意:古典的な方法でエンティティを生成したとき(つまり、doctrine:generate:entity)、データベースに3つのテーブルがありました。

Evgeniy Kuzmin

3つのクラスに単一のテーブルを使用する場合は、DiscriminatorMapを使用する必要があります

例:クラスProductと、Productから継承した2つの子クラスCouponと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 \ Entity \ Coupon

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

MyApp \ CusomProductBundle \ Entity \ 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 \ Entity \ Coupon

/**
 * @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
{
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事