2016-03-22 4 views
0

商品、ProductFeatures(Color、Sizeなど)、ProductFeaturesVarient(赤、オレンジ、緑、黄、32,34,36など)の3つのエンティティがあります。Symfony 2 + Doctrine、複数のテーブルを結合する

製品には色、サイズなどの機能があります。各機能にはさまざまなバリエーションがあります。レッド、オレンジ色、32、34サイズのポロTシャツのように。

これら3つのエンティティを結合テーブルで関連付けたいと考えています。私はProductFeatureVariantのようなエンティティを作成しなければならないことを知っていますが、関係をどのように定義するかはわかりません。

したがって、フィールド:product_id、feature_id、およびfeature_variant_idを持つ4番目のエンティティProductFeatureVariant。

誰でもこの定義に役立つことができますか?

+0

通常、自分で結合テーブルを作成する必要はありません。関係を使用する(製品と機能、色と機能、サイズなど) – JimL

+0

テーブル名のように見えますが混乱します。ここで私は を期待しています基本的な構造は、製品** **です:ID、Feature_title ** FeatureVariant **:ID、FEATURE_ID、variant_title 決勝の組み合わせ表のようになりますID、名前 **は**特長: ** ProductFeatureVariant **:product_id、feature_id、およびvariant_id –

答えて

0

あなたは3対1の関係を必要としますが、これは双方向にすることができます。

/** @Entity */ 
class Product 
{ 
    /** @Column(type="integer") */ 
    private $id; 
    /** @Column(length=140) */ 
    private $name; 
    /** 
    * @OneToMany(targetEntity="ProductFeature", mappedBy="product") 
    */ 
    private $features; 

    public function __construct() { 
     $this->features = new ArrayCollection(); 
    } 
} 

/** @Entity */ 
class ProductFeature 
{ 
    /** @Column(type="integer") */ 
    private $id; 
    /** @Column(length=140) */ 
    private $name; 
    /** 
    * @ManyToOne(targetEntity="Product", inversedBy="features") 
    * @JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    private $product; 
    /** 
    * @OneToMany(targetEntity="ProductFeatureVariant", mappedBy="productFeature") 
    */ 
    private $variants; 

    public function __construct() { 
     $this->variants = new ArrayCollection(); 
    } 
} 

/** @Entity */ 
class ProductFeatureVariant 
{ 
    /** @Column(type="integer") */ 
    private $id; 
    /** @Column(length=140) */ 
    private $name; 
    /** 
    * @ManyToOne(targetEntity="ProductFeature", inversedBy="variants") 
    * @JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    private $productFeature; 
} 

単純な結合を行うことができます。

//select products with a certain color 
$qb = $this->entityManager->createQueryBuilder(); 
$qb 
    ->select('p') 
    ->from('Product', 'p') 
    ->leftJoin('p.features', 'f') 
    ->leftJoin('f.variants', 'v') 
    ->where('v.name = :color') 
    ->setParameter('color', $color); 
0

ProductFeaturesVariantはすでにそれが製品とProductFeaturesVariantテーブルの間だけで多対多の関係になるので、私はProductFeaturesテーブルへの参照を避けるだろうProductFeaturesにリンクされています。 ProductFeaturesVariantを他のフィーチャに再割り当てすると、トリプルリンクテーブルが矛盾するようになります。

関連する問題